Aditya
Aditya

Reputation: 195

Execute a shell script in runtime of Java code from Eclipse

I have a shell script that I want to execute in Eclipse from a Java code.

I am able to use external tools in order to run the script, but I want the script to run during the execution of the Java code i.e. the Java code should make a call to the script.

Earlier I was using the "Process Builder" to do so but it seems Eclipse does not support that method as it says "File not found" when I have given it all the permissions.

Any idea how to run the script from a Java code in Eclipse?

Here is the code:

        String line1 = null;
        String target = new String("/home/aditya_s/workspace/rs-test/src/iperf_parse.sh " + host + " " + "9000");
        while(line1 == null)
        {
            Process p1 = Runtime.getRuntime().exec(target);
            BufferedReader input1 = new BufferedReader(new InputStreamReader(p1.getInputStream()));
            System.out.println("Ran");
            line1 = input1.readLine();
            p1.destroy();
        }
        BW = Double.parseDouble(line1);

TIP: Right click on the shell script and go to Properties. There give the 'Read/Write/Execute' permissions to 'Owner'. By default, the 'Execute' permission is not there. So you might get an error like 'Could not execute script.sh'.

Upvotes: 1

Views: 5689

Answers (3)

WouterH
WouterH

Reputation: 1356

Eclipse runs your java program just as it normally would, so ProcessBuilder will work as well. It is possible that you passed in the wrong path. If the path is relative, it will be relative to System.getProperty("user.dir").

Upvotes: 1

Jeevan Patil
Jeevan Patil

Reputation: 6089

You should look at ProcessBuilder

Upvotes: 0

David
David

Reputation: 20073

You should be able to use the below, replace shellscript with the script you want to run.

String s = "shellscript";
Process proc = Runtime.getRuntime().exec(s); 

Upvotes: 0

Related Questions