可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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);
回答1:
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 };
回答2:
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'"
回答3:
make sure of the name of your interface
use netsh interface ipv4 show config
in cmd to check the name of your connection
回答4:
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.
回答5:
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.