使用Java在cmd中执行命令

时间:2015-12-12 11:30:02

标签: java

我使用以下代码运行cmd.exe,然后出现cmd窗口,这很好。

  boolean movePlayerWithMouse = false;

  while(Keyboard.next()) {   
      if(!Keyboard.getEventKeyState()) {
          if(Keyboard.getEventKey() == Keyboard.KEY_TAB) {
              movePlayerWithMouse = !movePlayerWithMouse;
             Mouse.setGrabbed(!Mouse.isGrabbed());
          }
      }
  }
  if(movePlayerWithMouse = !movePlayerWithMouse) {
     rotY -= Mouse.getDX() / SENSITIVITY;
  }

image 1

我的问题是如何在使用java之前在cmd上运行以下命令

Runtime runtime = Runtime.getRuntime();
    try {
        Process p = runtime.exec("cmd.exe /c start");
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

我的目标是让命令行看起来像下面的图像

imag2

1 个答案:

答案 0 :(得分:2)

运行

Runtime runtime = Runtime.getRuntime();      
try { 
    Process p = runtime.exec("start cmd.exe /k \"ghci.exe test.hs\""); 

} catch (IOException ex) {
 Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
}

referens http://ss64.com/nt/cmd.html

How to open the command prompt and insert commands using Java?

的更多信息