You should really look at Process Builder. It is really built for this kind of thing.

ProcessBuilder pb = new ProcessBuilder("myshellScript.sh", "myArg1", "myArg2");
 Map<String, String> env = pb.environment();
 env.put("VAR1", "myValue");
 env.remove("OTHERVAR");
 env.put("VAR2", env.get("VAR1") + "suffix");
 pb.directory(new File("myDir"));
 Process p = pb.start();
Answer from Milhous on Stack Overflow
🌐
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
This post talks about how you can execute a shell script from a Java program. If you have a shell script say test.sh then you can run it from a Java program using RunTime class or ProcessBuilder (Note ProcessBuilder is added in Java 5).
🌐
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
🌐
Kevin Boone
kevinboone.me › exec.html
How to run a shell script from a Java application - Kevin Boone
If the script is in the working directory of the Java application, you probably need to do this: ... Alternatively, you could use the full pathname of the script. It should be obvious, but bear in mind that running a script using Java doesn't do any additional magic.
Find elsewhere
🌐
DEV Community
dev.to › coder4_life › how-to-run-shell-scripts-in-java-4cbd
How to run shell scripts in Java - DEV Community
December 6, 2021 - Running shell scripts from inside Java code using ProcessBuilder in a thread.
🌐
Ask Ubuntu
askubuntu.com › questions › 1050600 › execute-sh-file-with-java-command
18.04 - Execute sh file with java command - Ask Ubuntu
Translated I'm looking for a similar solution to creating a bat file in Windows on Ubuntu 18.04, I have a java file in which I make the call via console with the command java -jar routine.jar howe...
🌐
W3Docs
w3docs.com › java
How to run Unix shell script from Java code?
To run a Unix shell script from Java code, you can use the Runtime.getRuntime().exec() method to execute the script.
🌐
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
🌐
Edureka Community
edureka.co › home › community › categories › java › how to run unix shell script from java code
How to run Unix shell script from Java code | Edureka Community
October 26, 2018 - It is quite simple to run a Unix command from Java. Runtime.getRuntime().exec(myCommand); But ... to run a shell script from within Java code?
🌐
Unix.com
unix.com › shell programming and scripting
Running shell script from java - Shell Programming and Scripting - Unix Linux Community
October 19, 2017 - Here is my simple Java class, which resides in different directory then my shell script. So I am trying to change path and run the shell script located there. Also, I am passing parameters to shell script from Java as arguments. But for some reasons ...I am getting IOException .
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();
    }
🌐
Mkyong
mkyong.com › home › java › how to execute shell command from java
How to execute shell command from Java - Mkyong.com
January 3, 2019 - The .sh only contains one line of shell script ( a simple ‘ pm enable {packagename} ‘ command ). Would it be easier to put the command somewhere, or use the ‘ test.sh ‘ var? The best option is to make a link from text in an existing app (decompiled and manipulated in .smali dorm, via ‘APKEditor)… This was posted on a couole of sites, without solution. Hope you can help; cheers! ... Hello, When i run java from terminal than my shell command executes.
🌐
GitHub
github.com › fabric8io-images › run-java-sh
GitHub - fabric8io-images/run-java-sh: Universal startup script for plain Java applications · GitHub
Usage: java -jar run-java.jar <command> with the following commands: help : This help message copy <file> : Write run-java.sh out to this file or directory readme : Print the README exec <arg> : Execute the script directly from the JVM.
Starred by 314 users
Forked by 84 users
Languages   Shell 77.1% | Java 21.7% | Dockerfile 1.2%
🌐
Baeldung
baeldung.com › home › java › core java › how to run a shell command in java
How to Run a Shell Command in Java | Baeldung
January 8, 2024 - Here, we created a new sub-process with .newSingleThreadExecutor() and then used .submit() to run our Process containing the shell commands. Additionally, .submit() returns a Future object we utilize to check the result of the process. Also, make sure to call the .get() method on the returned object to wait for the computation to complete. If you are running the above code from a main method, be sure to call shutdown on the executorService object or the code will never stop.
🌐
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 ...