[英]Executing command line using “Runtime.getRuntime().exec()”
I'm having a problem while executing a command line from Java code. 从Java代码执行命令行时遇到问题。
This is the code I use, and under the code are the errors I got. 这是我使用的代码,在代码下是我遇到的错误。
I also tried using ProcessBuilder
but I got the same errors. 我也尝试使用
ProcessBuilder
但出现了相同的错误。
Ps: I am running the program on Windows. 附:我正在Windows上运行该程序。
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Process compilerProcess = Runtime.getRuntime().exec("clafer -k -m choco clafer.cfr);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(
compilerProcess.getInputStream()));
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.print(s); }
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// To have access to the text generated by the compiler.
}
}
The error: 错误:
java.io.IOException: Cannot run program "echo": CreateProcess error=2, the
specified file is not found
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at eqe.Test.main(Test.java:12)
Caused by: java.io.IOException: CreateProcess error=2, the specified file
is not found
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
I found the solution to the problem. 我找到了解决问题的办法。 I simply put the command line into new String[] and worked.
我只是将命令行放入新的String []中并开始工作。
Here is the solution for using to command line that allows you to execute long command line. 这是使用命令行的解决方案,允许您执行较长的命令行。
Process compilerProcess = Runtime.getRuntime().exec(new String[]{"clafer","-k","-m","choco","clafer.cfr" });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.