首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过java调用外部程序

问通过java调用外部程序
EN

Stack Overflow用户
提问于 2016-02-17 11:40:43
回答 3查看 1.6K关注 0票数 2

我想通过java运行NS2(这是linux中的一个外部程序)命令,当我得到ns命令没有找到错误时,使用ProcessBuilder。

代码语言:javascript
复制
/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代码是

代码语言:javascript
复制
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);
}
}
}

我想我的节目打得不对。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-02-17 11:55:19

我猜jvm执行命令的终端/会话不知道什么是ns‘,即:您的可执行库。

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

代码语言:javascript
复制
Process p = Runtime.getRuntime().exec( "/fullpath/ns /home/maria/ns-allinone-2.35/ns-....
票数 1
EN

Stack Overflow用户

发布于 2016-02-17 12:05:05

如果NS是一个java程序,那么使用java ns如下所示。

代码语言:javascript
复制
Runtime.getRuntime().exec( "java 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");
票数 1
EN

Stack Overflow用户

发布于 2016-02-19 16:11:54

Java找不到'ns‘。要告诉Java程序,您必须提到完整的路径,如下所示:

代码语言:javascript
复制
Process p = Runtime.getRuntime().exec("/home/maria/ns-allinone-2.35/ns-2.35/indep-utils/cmu-scen-gen/setdest/setdest -v 2 -n 50 -p 2.0 -s 10.0 -t 200 -x 500 -y 500 -m 2 -M 15");
Process p = Runtime.getRuntime().exec("/home/maria/ns-allinone-2.35/bin/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");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35455695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档