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);
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.
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
the error was found on this line:
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.
You (probably) need to correctly concatenate those
key=value
arguments - as written they'll be treated as separate arguments, i.e.make sure of the name of your interface
use
netsh interface ipv4 show config
in cmd to check the name of your connectionHave you tried this?
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: