java.io.IOException: 无法运行程序 "dir": CreateProcess error=2, Das System
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21281354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.io.IOException: Cannot run program "dir": CreateProcess error=2, Das System
提问by user2889693
Hello I try to run the following cmd code in eclipse:
您好,我尝试在 eclipse 中运行以下 cmd 代码:
"DIR \""+DEV_HOME+"\src\"\*.java /b /s >> \""+DEV_HOME+"\bin\javaFiles.txt\""
In clear it looks like this:
很明显,它看起来像这样:
DIR "D:\Thomas\Dokumente\Daten\workspace\WBRLight\src"\*.java /b /s >> "D:\Thomas\Dokumente\Daten\workspace\WBRLight\bin\javaFiles.txt"
But I get following error message:
但我收到以下错误消息:
java.io.IOException: Cannot run program "dir": CreateProcess error=2, Das System kann die angegebene Datei nicht finden
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
at java.lang.Runtime.exec(Runtime.java:617)
at java.lang.Runtime.exec(Runtime.java:450)
....
When I try to use the code in the cmd box, Its working fine. My code:
当我尝试使用 cmd 框中的代码时,它工作正常。我的代码:
public void run_cmdLine(String command) {
try {
Runtime rt = Runtime.getRuntime();
BufferedReader input = null;
Process pr = null;
pr = rt.exec(command);
input = new BufferedReader(new inputStreamReader(pr.getInputStream()));
String line = null;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code " + exitVal);
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
采纳答案by jeroen_de_schutter
Add "cmd.exe /c"
at the beginning of your command string, that should do the trick.
"cmd.exe /c"
在命令字符串的开头添加,这应该可以解决问题。
Edit: The /c
parameter will make the cmd
finish an return it to the java process. Without it, the process will hang.
编辑:该/c
参数将使cmd
完成并将其返回给 java 进程。没有它,进程将挂起。