java.io.IOException:无法运行程序“set”:CreateProcess error=2,系统找不到指定的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36447720/
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 "set": CreateProcess error=2, The system cannot find the file specified
提问by HimaaS
I am trying to run set command from eclipse, but i am getting the below exception.
我正在尝试从 Eclipse 运行 set 命令,但出现以下异常。
java.io.IOException: Cannot run program "set": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at java.lang.Runtime.exec(Runtime.java:450)
at java.lang.Runtime.exec(Runtime.java:347)
Here is my piece of code:
这是我的一段代码:
String command = "set Path=C:/Program Files/Java/jdk1.6.0_21/bin";
Process p = Runtime.getRuntime().exec(command);
回答by wero
The program fails because set
is not an executable but a command inside the command processor cmd.exe
.
程序失败,因为set
它不是可执行文件,而是命令处理器内的命令cmd.exe
。
To invoke it use
调用它使用
String command = "cmd.exe /c set path=C:/Program Files/Java/jdk1.6.0_21/bin";
Process p = Runtime.getRuntime().exec(command);
But be aware of the pitfalls of setting environment variables, see How to set an environment variable in Java using exec?as mentionend in the comments by @Berger
但请注意设置环境变量的陷阱,请参阅如何使用 exec 在 Java 中设置环境变量?正如@Berger 在评论中提到的那样