I am trying to execute a bash file through java in Ubuntu14.0. Bash file has the the code to generate a mobility model for NS2
"#!/bin/bash +x
cd /home/maria/ns-allinone-2.35/ns-2.35/indep-utils/cmu-scen-gen
ns cbrgen.tcl -type cbr -nn 10 -seed 1 -mc 5 -rate 5.0"
file is executable and giving output if run explicitly through terminal.
But when run through java its gives following 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)
This is my code:
package test;
import java.io.IOException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
public class opencmd {
int iExitValue;
String sCommandString;
public void runScript(String command){
sCommandString = command;
CommandLine oCmdLine = CommandLine.parse(sCommandString);
DefaultExecutor oDefaultExecutor = new DefaultExecutor();
oDefaultExecutor.setExitValue(0);
try {
iExitValue = oDefaultExecutor.execute(oCmdLine);
} catch (ExecuteException e) {
System.err.println("Execution failed.");
e.printStackTrace();
} catch (IOException e) {
System.err.println("permission denied.");
e.printStackTrace();
}
}
public static void main(String args[]){
opencmd testScript = new opencmd();
testScript.runScript("bash /home/maria/Documents/test.sh");
}
}