1

I am almost dead trying to sort this out.. Can someone help... please?

Below is the code:

import java.io.*;
import java.lang.Runtime;
import java.util.*;

public class WORKBRO {  

    public static void main(String args[])
    {
        try
        {    
            String target = new String("/home/dhirendra.panwar/Desktop/test.sh");
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec(target);

        } catch (Throwable t)
        {
            t.printStackTrace();
        }
    }
}
rghome
  • 8,529
  • 8
  • 43
  • 62
Dhirendra Panwar
  • 21
  • 1
  • 1
  • 4

1 Answers1

6

Your code is right and I am sure you are not getting exceptions, if you read using proc.getErrorStream() you will not get anything.
Commands 100% get executed that way, having said that now thing is that you are echo'ing something and you need to read it back using BufferedReader.

Check below example which will successfully create a directory called "stackOverflow" and print what you are echo'ing. For the putting it into a log file I am afraid that you can do it using ">", you may have to use some editor command or create file using Java.

Bottom line: Runtime.getRuntime().exec("command") is the correct and defined way to execute Unix commands or scripts from Java and it WORKS.

test.sh

#!/bin/bash
echo "hola"
mkdir stackOverflow

Test.java

import java.io.*;
public class Test {

        public static void main(String[] args) throws Exception {
                try {
                        String target = new String("/home/hagrawal/test.sh");
// String target = new String("mkdir stackOver");
                        Runtime rt = Runtime.getRuntime();
                        Process proc = rt.exec(target);
                        proc.waitFor();
                        StringBuffer output = new StringBuffer();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                        String line = "";                       
                        while ((line = reader.readLine())!= null) {
                                output.append(line + "\n");
                        }
                        System.out.println("### " + output);
                } catch (Throwable t) {
                        t.printStackTrace();
                }
        }
}
hagrawal7777
  • 14,103
  • 5
  • 40
  • 70
  • Hey thanks for the confirmation that this surely works... I tried your code.. hola is getting printed on console that is fine... it means script is getting executed.. but the directory "stackOverflow" is not getting created... Its insane... if the script is running then that directory should also get created... What can it be...? – Dhirendra Panwar Jun 04 '15 at 05:16
  • 1
    In the test.sh file have to give the absolute path where that folder should get created... #!/bin/bash echo "hola" mkdir /tmp/stackOverflow – Dhirendra Panwar Jun 04 '15 at 06:04
  • Is it good practice to call scripts from JAVA? Any performance issues? – kautuksahni Sep 18 '17 at 05:59
  • @kautuksahni I think it is not "question" of practice but question of "use-case", if you are working on Java program and you need to call a script or execute a command to on kernel then you have to do it, it really depends whether you can achieve it other way round, and it is not uncommon to call scripts from Java code. I am sure what do you mean by performance issue in this context. – hagrawal7777 Sep 18 '17 at 09:50
  • @hagrawal Thanks for the prompt reply and it is certainly informative. When I say performance issues it is with respect to JAVA-3rd party REST calls VS java-Python(script call). I have seen many people calling the python script from the JAVA rather than creating a scalable REST based architecture with 3rd party language (Python). I am very new to the JAVA world so please forgive me for my lack of knowledge in it. But I would like to understand the call stack of JAVE-Script Call(if you can provide me with it.). – kautuksahni Sep 19 '17 at 04:14