我已经看过几个关于这个的话题,但我没有让我工作.我想要做的就是从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)
试试这个..
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)
| 归档时间: |
|
| 查看次数: |
19078 次 |
| 最近记录: |