我已经看到了几个与此相关的主题,但我还没有开始工作。 我想做的就是从 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());
}
}
}
最佳答案
试试这个..
public static void main(String args[]) {
try {
Runtime.getRuntime().exec("cmd.exe /c start");
System.out.println("ok");
} catch (IOException ex) {
ex.printStackTrace();
}
}
关于java - 如何在java中打开cmd.exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20120945/