2015-03-25 106 views
1

我想斷開我的電腦與互聯網的連接,而我能想到的最佳方式是從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 

這我不知道是什麼意思。

任何想法我做錯了什麼?任何更好的選擇從路由器斷開?

+1

不應該是'ipconfig/release'(注意空間),一般來說,通常使用'ProcessBuilder'並將每個命令和參數分隔爲自己的參數'new ProcessBuilder(「ipconfig」, 「/ release」);' – MadProgrammer 2015-03-25 05:32:38

+0

@MadProgrammer如果你在Windows上,請從命令行嘗試'ipconfig/release' – 2015-03-25 05:33:44

+2

Windows命令行具有錯誤兼容的行爲,可以在斜槓上分割令牌。 'exec'也是一樣的, – chrylis 2015-03-25 05:35:31

回答

0

它的工作原理與空間

Process result = Runtime.getRuntime().exec("ipconfig /release"); 
+0

你也可以用C:/ Windows/System32/ipconfig .exe/release,給出ipconfig的完整路徑 – Kick 2015-03-25 05:43:31

0

假設IPCONFIG.EXE在PATH,那麼的Runtime.exec能夠找到它,如果它不是,你將需要提供的完全限定路徑到它。

相關問題