Java 使用“执行命令行”;Runtime.getRuntime().exec();
从Java代码执行命令行时出现问题。Java 使用“执行命令行”;Runtime.getRuntime().exec();,java,eclipse,command-line,Java,Eclipse,Command Line,从Java代码执行命令行时出现问题。 这是我使用的代码,下面是我得到的错误。 我还尝试使用ProcessBuilder,但也遇到了同样的错误 Ps:我正在Windows上运行这个程序 public class Test { public static void main(String[] args) { // TODO Auto-generated method stub try { Process compilerProcess = Runtime.getRu
这是我使用的代码,下面是我得到的错误。
我还尝试使用
ProcessBuilder
,但也遇到了同样的错误
Ps:我正在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.
}
}
错误:
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
java.io.IOException:无法运行程序“echo”:CreateProcess错误=2
找不到指定的文件
位于java.lang.ProcessBuilder.start(未知源)
位于java.lang.Runtime.exec(未知源)
位于java.lang.Runtime.exec(未知源)
位于java.lang.Runtime.exec(未知源)
位于eqe.Test.main(Test.java:12)
原因:java.io.IOException:CreateProcess错误=2,指定的文件
找不到
在java.lang.ProcessImpl.create(本机方法)
位于java.lang.ProcessImpl。(未知源)
位于java.lang.ProcessImpl.start(未知源)
... 还有5个
我找到了问题的解决方案。我只是简单地将命令行放入新字符串[]中并运行。下面是使用to命令行的解决方案,它允许您执行长命令行
Process compilerProcess = Runtime.getRuntime().exec(new String[]{"clafer","-k","-m","choco","clafer.cfr" });
Windows没有
echo
。在cmd中有…但这不是问题所在。它是一个内置的cmd
命令。不是外部程序。在*NIX上,它是一个独立的程序。因此,例如,当我在java类中使用命令help时,它确实可以工作……那么为什么其他命令行不能完美地工作呢cmd@Joe17如果你有能力,请随时为他人回答你自己的问题。