You should use the returned Process to get the result.

Runtime#exec executes the command as a separate process and returns an object of type Process. You should call Process#waitFor so that your program waits until the new process finishes. Then, you can invoke Process.html#getOutputStream() on the returned Process object to inspect the output of the executed command.

An alternative way of creating a process is to use ProcessBuilder.

Process p = new ProcessBuilder("myCommand", "myArg").start();

With a ProcessBuilder, you list the arguments of the command as separate arguments.

See Difference between ProcessBuilder and Runtime.exec() and ProcessBuilder vs Runtime.exec() to learn more about the differences between Runtime#exec and ProcessBuilder#start.

Answer from reprogrammer on Stack Overflow
๐ŸŒ
Medium
frankielc.medium.com โ€บ run-bash-commands-from-java-8319fbff23f7
Run bash commands from Java - Frankie - Medium
March 1, 2023 - As one of Java paradigms is โ€œwrite once, run anywhereโ€, and calling external software makes that much more complicated, youโ€™ll want to steer away from it as much as possible. However, sometimes thereโ€™s really no other viable option. The example below shows how to ping a host and capture both the standard and the error output streams. String cmd = "ping -c 4 -W 1 8.8.8.8" try { ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd); pb.redirectErrorStream(true); Process process = pb.start(); process.waitFor(10, TimeUnit.SECONDS); try (BufferedReader br = new BufferedReader( new Input
๐ŸŒ
Stack Exchange
unix.stackexchange.com โ€บ questions โ€บ 237104 โ€บ how-to-run-java-program-in-bash-script-and-give-it-one-argument
directory - How to run Java program in Bash script and give it one argument? - Unix & Linux Stack Exchange
October 19, 2015 - I know about that Java thingy., in fact, it's not about Java - but how to make the OS, run a program, in a way that I want, with all requirements supplied. ... There is Caja-actions Configration tool to add the open with ABC in context menu. There is command tab in Caja Action tool, there you can provide the script path and the directory argument.
Top answer
1 of 3
5

Another way of doing would be to use Runtime.getRuntime(). Something like this

  public void executeScript() throws IOException, InterruptedException {
    Process p = Runtime.getRuntime().exec("sh /root/Desktop/chat/script.sh");
    p.waitFor();

    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    BufferedReader errorReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));


    String line = "";
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }

    line = "";
    while ((line = errorReader.readLine()) != null) {
        System.out.println(line);
    }
}
2 of 3
1

With the above test you can not guaranty that whether it is running or not. Because clearly you told that your are running a infinite loop inside your second java application. Now my advise would be to put some System.out.println statement inside that infinite loop and use below java code to execute your shell script. Here in the output.txt file you can see the output from your shell script as well as java program and you will know whether application executed successfully or not. Also put some echo statement inside your shell script as well.

 String[] command ={"/root/Desktop/chat/script.sh", "command line param if any"};
    ProcessBuilder pb = new ProcessBuilder(command);

    pb.redirectOutput(new File("/tmp/output.txt"));
    String result;
    String overall="";
    try {
        Process p = pb.start();
        p.waitFor();
        BufferedReader br = new BufferedReader(
                new InputStreamReader(p.getInputStream()));
            while ((result = br.readLine()) != null){
                overall = overall + "\n" + result;
            }
            p.destroy();
            System.out.println(result);

    } catch (Exception e) {
        e.printStackTrace();
    }
๐ŸŒ
Quora
quora.com โ€บ How-do-I-write-a-bash-script-to-run-a-Java-program-with-a-series-of-arguments-automatically
How to write a bash script to run a Java program with a series of arguments, automatically - Quora
Answer: Enter the following in a text file using a text editor. Naturally, substitute the name of your program and the accordant arguments. Type it exactly the way you would do it on a shell commandline. Lets say you name it [code ]myProgram.sh[/code] : [code]#! /bin/bash # since this is a java ...
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ How-do-I-run-a-shell-script-from-Java-code
How to run a shell script from Java code - Quora
Answer (1 of 7): Here are multiple ways already suggested in stackoverflow and both are good: How to run Unix shell script from java code? Run shell script from Java Synchronously
๐ŸŒ
Netjstech
netjstech.com โ€บ 2016 โ€บ 10 โ€บ how-to-run-shell-script-from-java-program.html
How to Run a Shell Script From Java Program | Tech Tutorials
You are trying to run shell script on a Windows system! What do you expect ??? Though there are ways to run sh from widows but that will require some work. You can't just rum a sh file from windows command line. Delete ... If you are using Windows replace "sh" with "cmd". The terminal of is "powershell" or "cmd". "sh", "/bin/bash", "/usr/bin/python" for other kinds of scripts.ReplyDelete
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to run shell scripts in Java - YouTube
Running shell scripts from inside Java code using ProcessBuilder in a thread. This solution works on Windows (.bat file) and Unix (.sh file) - running exampl...
Published ย  December 6, 2021
๐ŸŒ
GitHub
gist.github.com โ€บ 4283217
Executing a linux bash command from a java program and reading the response of it which spans multiple lines ยท GitHub
Clone this repository at <script src="https://gist.github.com/rgurubha/4283217.js"></script> Save rgurubha/4283217 to your computer and use it in GitHub Desktop. ... Executing a linux bash command from a java program and reading the response of it which spans multiple lines
๐ŸŒ
IDRSolutions
blog.idrsolutions.com โ€บ home โ€บ tutorial โ€“ calling java from a bash script
Tutorial โ€“ Calling Java from a Bash Script
November 10, 2025 - And finally, the code will run the PDFtoHTML5 jar with the input PDF file name and the output location. ... echo "---------Converting---------" java -Xmx512M -jar jpdf2html.jar $FILE $OUTPUT echo echo "---------Finished Script---------" ... #!/bin/bash clear echo "---------Starting Script---------" echo echo "---------Generating Vars---------" OUTPUT="OutputFile/" FILE="examplePDF.pdf" echo "---------Making Directories---------" mkdir -p OutputFile/ echo "---------Converting---------" java -Xmx512M -jar jpdf2html.jar $FILE $OUTPUT echo echo "---------Finished Script---------"
๐ŸŒ
Kevin Boone
kevinboone.me โ€บ exec.html
How to run a shell script from a Java application - Kevin Boone
Running a shell script from a Java program using Runtime.exec() appears simple. In practice, there are many pitfalls. This article describes how to avoid at least some of them.
๐ŸŒ
W3Docs
w3docs.com โ€บ java
How to run Unix shell script from Java code?
String scriptPath = "/path/to/... } ... In this example, the exec() method is used to execute the script.sh script, and the waitFor() method is used to wait for the script to finish executing....
๐ŸŒ
GitHub
gist.github.com โ€บ simonwoo โ€บ c21811eddf0392034946
execute a shell script from java.md ยท GitHub
/** * execute shell script */ private static void executeScript() { try { String bash = "/bin/bash"; String script = "script.sh"; String[] command = { bash, script }; System.out.println("Starting execute the script"); ProcessBuilder processBuilder ...