java - 通过java调用外部程序

标签 java linux ns2

我想使用ProcessBuilder通过java运行NS2(Linux中的外部程序)命令 当我收到 ns 命令未找到错误时

/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);
}
}
}

我想我没有进行正确的程序调用。

最佳答案

我猜jvm执行你的命令的终端/ session 不知道'ns'在哪里/什么是[即:你的可执行库]

尝试通过提供库的完整路径来执行,例如

Process p = Runtime.getRuntime().exec( "/fullpath/ns /home/maria/ns-allinone-2.35/ns-....

关于java - 通过java调用外部程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35455695/

相关文章:

python - 在 Linux 上使用 Python 从子进程中杀死父进程

tcl - 错误 "Floating point exception (core dumped)"是什么原因

error-handling - 单独网络的LTE网络在ns2中出现错误

c++ - NS2:AODV 路由消息卡在队列中

java - Java 9 是否包含 Graal?

javax.xml.bind.JAXBElement 无法转换为 com.oracle.xmlns.communications.ordermanagement.CreateOrderRequestType

python - 通过 ssh 隧道远程运行 ipython notebook

java - 如何从 jsonArray 获取相同的字段

java - 在我的 REST Web 服务的服务层中使用 @EJB 或 @Inject 注入(inject) EJB?

linux - 资源属性可以依赖于文件存在吗?