Java Runtime.getRuntime()。exec(“ipconfig/release”);返回错误?
问题描述:
我想断开我的电脑与互联网的连接,而我能想到的最佳方式是从cmd运行ipconfig/release。要做到这一点,我做了Java Runtime.getRuntime()。exec(“ipconfig/release”);返回错误?
Process result = Runtime.getRuntime().exec("ipconfig/release");
这不过抛出一个错误,
java.io.IOException: Cannot run program "ipconfig/release": CreateProcess error=
2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at threadio.run(checkmac.java:141)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find th
e file specified
这我不知道是什么意思。
任何想法我做错了什么?任何更好的选择从路由器断开?
答
它的工作原理与空间
Process result = Runtime.getRuntime().exec("ipconfig /release");
+0
你也可以用C:/ Windows/System32/ipconfig .exe/release,给出ipconfig的完整路径 – Kick 2015-03-25 05:43:31
答
假设IPCONFIG.EXE在PATH,那么的Runtime.exec能够找到它,如果它不是,你将需要提供的完全限定路径到它。
不应该是'ipconfig/release'(注意空间),一般来说,通常使用'ProcessBuilder'并将每个命令和参数分隔为自己的参数'new ProcessBuilder(“ipconfig”, “/ release”);' – MadProgrammer 2015-03-25 05:32:38
@MadProgrammer如果你在Windows上,请从命令行尝试'ipconfig/release' – 2015-03-25 05:33:44
Windows命令行具有错误兼容的行为,可以在斜杠上分割令牌。 'exec'也是一样的, – chrylis 2015-03-25 05:35:31