Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过java调用外部程序_Java_Linux_Ns2 - Fatal编程技术网

通过java调用外部程序

通过java调用外部程序,java,linux,ns2,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

我想使用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执行您的命令的终端/会话不知道“ns”在哪里/是什么[即:您的可执行库]

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

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

如果
NS
是一个java程序,则使用
JavaNS
,如下所示

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");

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

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");

启动ns命令时,是否可以尝试将完整路径放入该命令?请在linux终端中尝试相同的命令。并检查它是否能在终端工作。但是在Java中给出了错误,我想这一定是解决方案。现在的问题是语法。如何传递到库的路径和在.exec()函数中运行的命令?thanx。NS不是java程序