Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output the result of a bash script

Tags:

java

linux

If for example I chose to run a bash script that would output (echo) the time e.g. CheckDate.sh. How could I run this from Java and then print the result of the bash script (the date) in my Java program?

like image 796
Mike Avatar asked Feb 21 '23 15:02

Mike


1 Answers

Try this code.

String result = null;
try {
    Runtime r = Runtime.getRuntime();                    

    Process p = r.exec("example.bat");

    BufferedReader in =
        new BufferedReader(new InputStreamReader(p.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
        System.out.println(inputLine);
        result += inputLine;
    }
    in.close();

} catch (IOException e) {
    System.out.println(e);
}
like image 154
Sunil Kumar B M Avatar answered Mar 03 '23 00:03

Sunil Kumar B M