如何在Java中执行Windows命令?

问题描述:

我正在开发一个项目,它会给你一个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

我希望这有助于:)

你可以使用:

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

一个例子。 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) { 
} 

利用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”)及其参数,这些参数会自动将呼叫附加到操作系统。我还包括让输出,可能是通过一个监听器回调接口连输入,如果该命令允许输入的能力...

老问题,但可能有助于有人经过。这是一个简单而有效的解决方案。上述某些解决方案不起作用。

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

这是一个示例代码运行并打印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/