2011-08-18 94 views
32

在Java中,我希望能夠執行Windows命令。如何使用Java執行Windows命令 - 更改網絡設置

有問題的命令是netsh。這將使我能夠設置/重置我的IP地址。

請注意,我不想執行批處理文件。

而不是使用批處理文件,我想直接執行這些命令。這可能嗎?


這裏是我的未來參考實施的解決方案:

public class JavaRunCommand { 
    private static final String CMD = 
     "netsh int ip set address name = \"Local Area Connection\" source = static addr = 192.168.222.3 mask = 255.255.255.0"; 
    public static void main(String args[]) { 

     try { 
      // Run "netsh" Windows command 
      Process process = Runtime.getRuntime().exec(CMD); 

      // Get input streams 
      BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); 
      BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); 

      // Read command standard output 
      String s; 
      System.out.println("Standard output: "); 
      while ((s = stdInput.readLine()) != null) { 
       System.out.println(s); 
      } 

      // Read command errors 
      System.out.println("Standard error: "); 
      while ((s = stdError.readLine()) != null) { 
       System.out.println(s); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(System.err); 
     } 
    } 
} 
+1

這已經回答了很多次,只要看看計算器建議找一些他們的 – SJuan76

+0

@ SJuan76,我道歉。你也許可以聯繫我的一些這些問題? – mre

+1

@mre只要看看在側邊欄。 –

回答

25
Runtime.getRuntime().exec("netsh"); 

請參閱Runtime Javadoc。

編輯:後來由leet回答表明,這個過程現在已被棄用。但是,根據DJViking的評論,這似乎並非如此:Java 8 documentation。該方法不被棄用。

+2

在我的環境中我不得不在命令前添加「cmd/c」,即'Runtime.getRuntime().exec(「cmd/c netsh」);' – Daniel

2
public static void main(String[] args) { 
    String command="netstat"; 
    try { 
     Process process = Runtime.getRuntime().exec(command); 
     System.out.println("the output stream is "+process.getOutputStream()); 
     BufferedReader reader=new BufferedReader(new InputStreamReader(process.getInputStream())); 
     String s; 
     while ((s = reader.readLine()) != null){ 
      System.out.println("The inout stream is " + s); 
     }     
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

This works。

+1

這會讀取輸出的每隔一行。 應該是: 'String s; ((s = reader.readLine())!= null){'是正確的('s = reader.readLine()!= null){ System.out.println(s); }' –

+0

' 。 – SuB

26

使用ProcessBuilder

ProcessBuilder pb=new ProcessBuilder(command); 
pb.redirectErrorStream(true); 
Process process=pb.start(); 
BufferedReader inStreamReader = new BufferedReader(
    new InputStreamReader(process.getInputStream())); 

while(inStreamReader.readLine() != null){ 
    //do something with commandline output. 
} 
+5

這個結論的基礎是什麼?查看javadoc,它沒有被宣告爲已被廢棄。 https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#exec -java.lang.String- – DJViking

+0

正在更新此內容:由於此問題在標記重複項時用作參考(即,我們將重複項指向此頁),因此澄清此答案將很好,因爲該答案更符合接受答案。是否有這種信息的來源?如果不是,可能應該刪除它作爲一個綁定的答案? – Nathan

+2

'Runtime.getRuntime().exec()'不被棄用。 – Daniel

0

您可以Runtime.getRuntime().exec("tree");運行命令。但是,這隻會運行在路徑中找到的可執行文件,而不是像echo,deldel,...但只有像tree.com,netstat.com,...這樣的命令要運行常規命令,必須在命令之前放置cmd /c(eaxmple:Runtime.getRuntime().exec("cmd /c echo echo");