如何在java中打开cmd.exe

Dan*_*iel 2 java cmd

我已经看过几个关于这个的话题,但我没有让我工作.我想要做的就是从java程序打开cmd.exe.

notepad.exe打开正常.

问题是cmd.exe剂量打开,代码编译正常,没有错误

这是我的代码:

public class CMD {

public static void main(String[] args) {

  //Trying some variants how to start. 

  //String cmd = "C:\\WINDOWS\\system32\\cmd.exe";
  //String[] cmd = {"C:\\WINDOWS\\system32\\cmd.exe","start"};

  String[] cmd = {"C:\\WINDOWS\\system32\\cmd.exe","/c","start"};

   // notepad works fine
   //String notepad = "C:\\WINDOWS\\system32\\notepad.exe";


 try {        
    Runtime runtime = Runtime.getRuntime();
    //Process p = runtime.exec(notepad);
    Process p = runtime.exec(cmd);


 }

catch (java.io.IOException exception) {
    System.out.println("Caught IOException: " + exception.getMessage());

    }
}
}
Run Code Online (Sandbox Code Playgroud)

sub*_*ash 7

试试这个..

public static void main(String args[]) {
    try {
        Runtime.getRuntime().exec("cmd.exe /c start");
        System.out.println("ok");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)