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
๐ŸŒ
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.
๐ŸŒ
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 - Process process; if (isWindows) { process = Runtime.getRuntime() .exec(String.format("cmd.exe /c dir %s", homeDirectory)); } else { process = Runtime.getRuntime() .exec(String.format("/bin/sh -c ls %s", homeDirectory)); } StreamGobbler streamGobbler = new StreamGobbler(process.getInputStream(), System.out::println); Future<?> future = executorService.submit(streamGobbler); int exitCode = process.waitFor(); assertDoesNotThrow(() -> future.get(10, TimeUnit.SECONDS)); assertEquals(0, exitCode); Here, we created a new sub-process with .newSingleThreadExecutor() and then used .submit() to run our Process containing the shell commands.
๐ŸŒ
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
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).
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ executing-shell-commands-with-java
Executing Shell Commands with Java
May 18, 2020 - Through it, the application itself communicates with the environment it's in. By extracting the runtime associated with our application via the getRuntime() method, we can use the exec() method to execute commands directly or run .bat/.sh files. The exec() method offers a few overloaded variations: ...
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ how to execute shell command from java
How to execute shell command from Java - Mkyong.com
January 3, 2019 - I used the same (similar) code for my ShellWrapper. When I use commands like echo it works fine, but when I use commands like cat it returns an empty Stringโ€ฆ. ... As a good practice, I wouldnโ€™t catch Exception e. Just mention the two that need to be dealt with (IOException, InterruptedException) . ... I am trying to convert avro to json file through java, But it is not working String output = obj.executeCommand(โ€œjava -jar /Users/xyz/Desktop/1server/jboss-as-7.1.1.Final/standalone/deployments/Command.war/WEB-INF/lib/avro-tools-1.7.7.jar tojson /Users/xyz/Desktop/avro/4.avro > /Users/xyz/Desktop/avro/D8EC9CC2A3E049648AFD4309B29D2A0F/4.jsonโ€); But this is not working through java file,
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-execute-native-shell-commands-from-java-program
How to Execute Native Shell Commands from Java Program? - GeeksforGeeks
March 3, 2021 - We use a list to build commands and then execute them using the "start" method of the ProcessBuilder class. The program runs the command to find the chrome browser processes from the tasklist running in the machine. ... // Run a simple Windows shell command import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class ShellCommandRunner { public static void main(String[] args) { ProcessBuilder processBuilder = new ProcessBuilder(); List<String> builderList = new ArrayList<>(); // add the list of comm
Find elsewhere
๐ŸŒ
ExtraVM
thishosting.rocks โ€บ how-to-execute-a-shell-command-using-java
How To Execute a Shell Command Using Java
May 19, 2021 - The exec() method is for executing commands directly or running .bat/.sh files. try { // -- Linux -- // Run a shell command // Process process = Runtime.getRuntime().exec("ls /home/mkyong/"); // Run a shell script // Process process = ...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 709609 โ€บ java โ€บ Executing-shell-script-java
Executing shell script in java (Java in General forum at Coderanch)
May 10, 2019 - But do you give the name and password as part of the command: $ <yourscript> some_user some_password Or does the script prompt for it: $ <yourscript> Please enter your username: some_user Please enter your password: some_password ? Finally, what does your code to execute and monitor the script currently look like? ... So I have a script like this: test.sh echo "hello login" read user read pw And the Java code above ^^ when I execute this script nothing happens it's like frozen
๐ŸŒ
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
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 379834 โ€บ java โ€บ executing-shell-script-java
executing shell script from java (Java in General forum at Coderanch)
October 10, 2016 - Read from child.inputStream() -- which is the shell's stdout -- or child.errorStream() -- which is stderr. String file_location = "/users/temp/rams"; String file_location1 = "/users/temp/Test.java"; String a_mib_name = "Test269.java"; Runtime rtime = Runtime.getRuntime(); Process child = rtime.exec("/bin/bash"); child.getOutputStream().write("cd " + file_location); System.out.println("cd " + file_location); child.getOutputStream().write("ln -s " + file_location1 + " " + a_mib_name); child.getOutputStream().flush(); System.out.println("inside script"); cd and softlink commands are not working...
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ java โ€“ run shell script on a remote server
Java - Run shell script on a remote server - Mkyong.com
October 1, 2020 - 1.2 In local, we can use the below code to run or execute the above shell script in a remote server. ... package com.mkyong.io.howto; import com.jcraft.jsch.*; import java.io.IOException; import java.io.InputStream; public class RunRemoteScript { private static final String REMOTE_HOST = "1.1.1.1"; private static final String USERNAME = ""; private static final String PASSWORD = ""; private static final int REMOTE_PORT = 22; private static final int SESSION_TIMEOUT = 10000; private static final int CHANNEL_TIMEOUT = 5000; public static void main(String[] args) { String remoteShellScript = "/ro
๐ŸŒ
GitHub
gist.github.com โ€บ simonwoo โ€บ c21811eddf0392034946
execute a shell script from java.md ยท GitHub
Clone this repository at &lt;script src=&quot;https://gist.github.com/simonwoo/c21811eddf0392034946.js&quot;&gt;&lt;/script&gt; Save simonwoo/c21811eddf0392034946 to your computer and use it in GitHub Desktop. ... /** * 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 = new ProcessBuilder(command); Map<String, String> env = processBuilder.environment(); env.put("ENV_KEY", "ENV_VALUE"); Process process =
๐ŸŒ
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. This solution works on...
๐ŸŒ
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.
๐ŸŒ
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 - Hello All, Hope all is well. I was trying to scratch my head here with simple problem of running Shell script in Java. I tried to google and look through forums but was unable to understand how to solve it. Here is my simple Java class, which resides in different directory then my shell script.