java 使用JAVA更改计算机IP地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26231227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Computer IP address using JAVA
提问by Zulkernain Tasin
I need to change computer IP address using java... I have tried this one but this doesnot work...
我需要使用java更改计算机IP地址...我已经尝试过这个但是这不起作用...
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);
回答by Alnitak
You (probably) need to correctly concatenate those key=value
arguments - as written they'll be treated as separate arguments, i.e.
您(可能)需要正确连接这些key=value
参数 - 正如所写的那样,它们将被视为单独的参数,即
{..., "addr1=" + str1, "mask=" + str2 };
回答by UniversE
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'"
回答by shehab khalid
make sure of the name of your interface
确保您的界面名称
use netsh interface ipv4 show config
in cmd to check the name of your connection
netsh interface ipv4 show config
在 cmd 中使用来检查您的连接名称
回答by N0X-3N
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
线程“main”中的异常java.lang.Error:未解决的编译问题:未处理的异常类型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.
我没有解决此问题的建议,但我可以说,查看提供的代码,除非用于形成循环,否则运行时似乎毫无用处,但由于您没有将 IP 设置为随机生成的数字,那将没有理由去做。
回答by N0X-3N
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
这似乎有效,但回报很奇怪: java.lang.ProcessImpl@659e0bfd
no errors are found and my ip has been altered, but not in an expected way.
没有发现错误,我的 ip 已被更改,但不是以预期的方式。