4

I need to change computer IP address using java... I have tried this one but this doesnot work...

    String str1="192.168.0.201"; 
    String str2="255.255.255.0";
    String[] command1 = { "netsh", "interface", "ip", "set", "address",
    "name=", "Local Area Connection" ,"source=static", "addr=",str1,
    "mask=", str2};
    Process pp = java.lang.Runtime.getRuntime().exec(command1);
7
  • any errors? Do you have permission? Does it work from the command line? Commented Oct 7, 2014 at 8:00
  • What does not work? Does it give you any error? Which one? Commented Oct 7, 2014 at 8:00
  • I dont know.. nothing is shown there... Commented Oct 7, 2014 at 8:01
  • didnt try that @ScaryWombat Commented Oct 7, 2014 at 8:02
  • 1
    @ZulkernainTasin what do you expect this code to do? This would change your local machine (ethernet) address. If you think that this would change your external IP address you're wrong. Commented Oct 7, 2014 at 8:08

6 Answers 6

2

You (probably) need to correctly concatenate those key=value arguments - as written they'll be treated as separate arguments, i.e.

{..., "addr1=" + str1, "mask=" + str2 };
Sign up to request clarification or add additional context in comments.

1 Comment

do the same with name=\"Local Area Connection\"
1

Have you tried this?

String[] command1 = { "netsh", "interface", "ip", "set", "address",
"name=\"Local Area Connection\"" ,"source=static", "addr="+str1,
"mask="+str2};

Note that now the arguments after the = are not separated by spaces. Also note the double quotation marks sourrounding Local Area Connection.

If this doesnt work either, try enclosing Local Area Connection in single quotation marks like this:

"name='Local Area Connection'"

Comments

1

make sure of the name of your interface

use netsh interface ipv4 show config in cmd to check the name of your connection

Comments

1

I wrote a library with JNA (Java 17) can do this , I hope anybody help me for improvements .
Note: Currently support Linux OS.
JSysBox

Current Features :

  • Networking :
    1. list available interfaces
    2. Set/Get interaface ip address
    3. Interface statistics
    4. IfUp/IfDown
    5. Add/Delete/List routes (specific method to set default gateway)
  • FileSystem:
    1. Mount/Umount/MountPoints Filesystems
    2. Set/Get system environments
    3. Set/Get hostname
  • Date and Time
    1. Set/Get system time zone
    2. Set/Get system date and time
    3. Sync system to hardware clock (like: hwclock --systohc)
    4. Sync hardware to system clock (like: hwclock --hctosys)

1 Comment

Repository available , you can see source code .
0
public class DaysinaMonth {
    public static void main(String[] args) throws Throwable{
        String str1="192.168.0.201"; 
        String str2="255.255.255.0";
        String[] command1 = { "netsh", "interface", "ip", "set", "address",
        "name=", "Local Area Connection" ,"source=static", "addr=",str1,
        "mask=", str2};
        Process pp = java.lang.Runtime.getRuntime().exec(command1);
        System.out.print( pp);
    }
}

This seems to work, but the returns are strange: java.lang.ProcessImpl@659e0bfd

no errors are found and my ip has been altered, but not in an expected way.

Comments

0

I tested out the code you posted, and here is the error I got

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type IOException

at DaysinaMonth.main(DaysinaMonth.java:9)

the error was found on this line:

Process pp = java.lang.Runtime.getRuntime().exec(command1);

I have no suggestions for fixing this, but I can say that looking at the code provided, the runtime seems to be useless unless used to form a loop, but since you didn't make the IP set as a randomly generated number, that would have no reason to be done.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.