【发布时间】:2016-05-29 02:20:52
【问题描述】:
我想通过 java 运行 NS2(Linux 中的外部程序)命令,使用 ProcessBuilder 当我得到 ns command not found 错误
/home/maria/Documents/test.sh: line 4: ns: command not found
Execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 127 (Exit value: 127)
at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:404)
at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166)
at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:153)
at test.opencmd.runScript(opencmd.java:18)
at test.opencmd.main(opencmd.java:30)
我的java代码是
package test;
import java.io.*;
public class test2 {
public static void main(String args[]) {
String s = null;
try {
// run the Unix "};
//System.out.print(System.getProperty("user.home"));
Process p = Runtime.getRuntime().exec( "ns /home/maria/ns-allinone-2.35/ns-2.35/indep-utils/cmu-scen-gen && cbrgen.tcl -type cbr -nn 10 -seed 1 -mc 5 -rate 5.0");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
}
我想我没有进行正确的程序调用。
【问题讨论】:
-
你可以尝试在启动时将完整路径放入ns命令吗?
-
请在 linux 终端中尝试相同的命令。并检查它是否会工作
-
它在终端中工作。但是在java中给出错误