Maria Khalid Maria Khalid - 1 year ago 126
Java Question

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/ 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(
at org.apache.commons.exec.DefaultExecutor.execute(
at org.apache.commons.exec.DefaultExecutor.execute(
at test.opencmd.runScript(
at test.opencmd.main(

my java code is

package test;
public class test2 {
public static void main(String args[]) {
String s = null;
try {
// run the Unix "};
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
BufferedReader stdError = new BufferedReader(new
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
// 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) {
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");

I guess i am not making a right program call.

Answer Source

I guess the terminal/session which jvm executes your command doesn't know where/what is 'ns' [i.e: 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-....