2013-05-09 146 views
2

我正在開發一個項目,它會給你一個Windows命令列表。當你選擇一個時,它將執行該命令。但是,我不知道該怎麼做。我打算用Visual C#或C++來做,但C++類太複雜了,我不想在Visual C#中製作窗體和垃圾(在控制檯應用程序中真的很糟糕)。如何在Java中執行Windows命令?

+1

搜索 「Java運行命令」,以幫助改進問題的更好 - 例如哪些部分存在問題?請注意,某些命令*在shell外部沒有意義*。這些包括'CD'等,並應相應地進行仿真。 (雖然,我可能會認爲這是一個「更好」的時間來模擬所有支持的命令投資 - 即移動/複製/列表/刪除 - ?在Java本身或者打開一個真正的外殼,讓用戶爲所欲爲) – user2246674 2013-05-09 01:37:41

+0

http://stackoverflow.com/questions/7112259/how-to-execute-windows-commands-using-java-change-network-settings – user2246674 2013-05-09 01:39:02

+0

另外,如果你是*月* Windows時,只需使用VS快遞(免費)+ C#(這和Java真的差不多)。它只是工作(TM),包括WinForms。 – user2246674 2013-05-09 01:41:26

回答

4

我希望這有助於:)

你可以使用:

Runtime.getRuntime().exec("ENTER COMMAND HERE"); 
4

一個例子。 1.創建cmd 2.寫入cmd - >調用命令。

try { 
    // Execute command 
    String command = "cmd /c start cmd.exe"; 
    Process child = Runtime.getRuntime().exec(command); 

    // Get output stream to write from it 
    OutputStream out = child.getOutputStream(); 

    out.write("cd C:/ /r/n".getBytes()); 
    out.flush(); 
    out.write("dir /r/n".getBytes()); 
    out.close(); 
} catch (IOException e) { 
} 
4

利用ProcessBuilder

這使得它更容易建立工藝參數和需要照顧的問題,具有自動命令空間...

public class TestProcessBuilder { 

    public static void main(String[] args) { 

     try { 
      ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "dir"); 
      pb.redirectError(); 
      Process p = pb.start(); 
      InputStreamConsumer isc = new InputStreamConsumer(p.getInputStream()); 
      isc.start(); 
      int exitCode = p.waitFor(); 

      isc.join(); 
      System.out.println("Process terminated with " + exitCode); 
     } catch (IOException | InterruptedException exp) { 
      exp.printStackTrace(); 
     } 

    } 

    public static class InputStreamConsumer extends Thread { 

     private InputStream is; 

     public InputStreamConsumer(InputStream is) { 
      this.is = is; 
     } 

     @Override 
     public void run() { 

      try { 
       int value = -1; 
       while ((value = is.read()) != -1) { 
        System.out.print((char)value); 
       } 
      } catch (IOException exp) { 
       exp.printStackTrace(); 
      } 

     } 

    } 
} 

我通常建立一個多用途類,你可以通過在「命令」(如「dir」)及其參數,這些參數會自動將呼叫附加到操作系統。我還包括讓輸出,可能是通過一個監聽器回調接口連輸入,如果該命令允許輸入的能力...

1

老問題,但可能有助於有人經過。這是一個簡單而有效的解決方案。上述某些解決方案不起作用。

import java.io.IOException; 
import java.io.InputStream; 

public class ExecuteDOSCommand 
{ 
    public static void main(String[] args) 
    { 
     final String dosCommand = "cmd /c dir /s"; 
     final String location = "C:\\WINDOWS\\system32"; 
     try 
     { 
      final Process process = Runtime.getRuntime().exec(dosCommand + " " + location); 
      final InputStream in = process.getInputStream(); 
      int ch; 
      while((ch = in.read()) != -1) 
      { 
       System.out.print((char)ch); 
      } 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

來源:http://www.devx.com/tips/Tip/42644

0

這是一個示例代碼運行並打印IPCONFIG命令的在控制檯窗口輸出。

import java.io.IOException; 
import java.io.InputStream; 

public class ExecuteDOSCommand { 
    public static void main(String[] args) { 
    final String dosCommand = "ipconfig"; 
     try { 
     final Process process = Runtime.getRuntime().exec(dosCommand); 
     final InputStream in = process.getInputStream(); 
     int ch; 
     while((ch = in.read()) != -1) { 
      System.out.print((char)ch); 
     }   
     } catch (IOException e) { 
     e.printStackTrace(); 
      } 
    } 
} 

來源:https://www.codepuran.com/java/execute-dos-command-java/