简体   繁体   中英

External program call through java

I want to run NS2(which is an external program in linux) commands through java, using ProcessBuilder when I get the ns command not found error

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

my java code is

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

I guess i am not making a right program call.

I guess the terminal/session which jvm executes your command doesn't know where/what is 'ns' [ie: your executable library]

Try executing by giving the full path to your library, like

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

如果NS是Java程序,则使用java ns ,如下所示。

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 is unable to find 'ns'. To tell that to Java program you have to mention full path as below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM