4

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");
    }
}
Neuron
  • 5,141
  • 5
  • 38
  • 59
Maria Khalid
  • 189
  • 2
  • 15
  • Are you running from within an IDE? What if you use the `/full/path/to/ns` instead? – Joao Morais Feb 17 '16 at 10:01
  • Yes I am using eclipse. I tried doing what you suggested but its giving error – Maria Khalid Feb 17 '16 at 10:04
  • Using the `/full/path/to/ns` works? – Joao Morais Feb 17 '16 at 10:05
  • 1
    java.io.IOException: Cannot run program "ns": error=2, No such file or directory at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048) at java.lang.Runtime.exec(Runtime.java:620) at java.lang.Runtime.exec(Runtime.java:450) at java.lang.Runtime.exec(Runtime.java:347) at test.test2.main(test2.java:10) Caused by: java.io.IOException: error=2, No such file or directory at java.lang.UNIXProcess.forkAndExec(Native Method) at java.lang.UNIXProcess.(UNIXProcess.java:248) at java.lang.ProcessImpl.start(ProcessImpl.java:134) – Maria Khalid Feb 17 '16 at 10:06
  • here is the codeProcess 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"); – Maria Khalid Feb 17 '16 at 10:07
  • It sounds that your `$PATH` isn't updated to the Eclipse's process, because of that I'm suggesting you to use the full path, something like `/usr/bin/ns`. Try `which ns` in the terminal in order to see the full path and use it in the script and in the Java code. – Joao Morais Feb 17 '16 at 10:11

1 Answers1

2

Please 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");
Bill
  • 172
  • 8