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 Top answer 1 of 16
187
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();
2 of 16
27
You can use Apache Commons exec library also.
Example :
package testShellScript;
import java.io.IOException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
public class TestScript {
int iExitValue;
String sCommandString;
public void runScript(String command){
sCommandString = command;
CommandLine oCmdLine = CommandLine.parse(sCommandString);
DefaultExecutor oDefaultExecutor = new DefaultExecutor();
oDefaultExecutor.setExitValue(0);
try {
iExitValue = oDefaultExecutor.execute(oCmdLine);
} catch (ExecuteException e) {
System.err.println("Execution failed.");
e.printStackTrace();
} catch (IOException e) {
System.err.println("permission denied.");
e.printStackTrace();
}
}
public static void main(String args[]){
TestScript testScript = new TestScript();
testScript.runScript("sh /root/Desktop/testScript.sh");
}
}
For further reference, An example is given on Apache Doc also.
Kevin Boone
kevinboone.me › exec.html
How to run a shell script from a Java application - Kevin Boone
To invoke the shell to run the script, we might try something like this: ... The -c switch tells the shell to process the next argument as a command. This won't work, either, because ... In my example, I want to invoke something.sh and pass it the argument 42.
Videos
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 - This class implements the Runnable interface, which means any Executor could execute it. Next, we’ll spawn a new process using the .exec() method and use the StreamGobler created previously. For example, we can list all the directories inside the user’s home directory and then print it to the console:
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 ... This method works fine when I try to execute on local.
Stack Abuse
stackabuse.com › executing-shell-commands-with-java
Executing Shell Commands with Java
May 18, 2020 - In this tutorial, we'll cover how to execute shell commands, bat and sh files in Java. We'll be covering examples for all exec() and ProcessBuilder approaches.
Medium
medium.com › se-notes-by-alexey-novakov › shell-script-in-java-8b3339be0280
Shell Script in Java. If you always wanted to write a shell… | by Alexey Novakov | SE Notes by Alexey Novakov | Medium
December 26, 2018 - Apart from the shebang file mode, one can run single-file source-code program via java command directly, for example: java -Dtrace=true --source 11 myprogram some_param1 · Let’s imagine some use case where we could apply this feature: Set Docker image tag for local Docker images and push them to specific Docker registry. Let’s solve it with with shell program in Java.
Coderanch
coderanch.com › t › 379834 › java › executing-shell-script-java
executing shell script from java (Java in General forum at Coderanch)
October 10, 2016 - Originally posted by Henry Wong: Take a look at the example that I showed you... You still have differences. The version of exec() that I am using is specifying a current working directory. One of the operations prior to executing the command is changing to the current working directory. Henry File file_location = new File("/users/temp/rag"); String file_location1 = "/users/temp/Test.java"; String a_mib_name = "Test269.java"; String cmd[] = new String[] {"/bin/bash", "-c", "ln", "-s", file_location1, a_mib_name}; Runtime rtime = Runtime.getRuntime(); Process child = rtime.exec(cmd, null, file_location); Wrong number of arguments in method.
Top answer 1 of 2
2
This is a simple code to execute the shell script and read its output:
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
String[] command = { "./flows.sh", "suspend" };
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String s;
while ((s = reader.readLine()) != null) {
System.out.println("Script output: " + s); // Replace this line with the code to print the result to file
}
}
}
To print it to a file, just replace the System.out.println for the code to write into a file
2 of 2
-1
You can use the JSCH API:
http://www.jcraft.com/jsch/examples/Shell.java.html http://www.jcraft.com/jsch/examples/Exec.java.html
Bye!
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?
Blogger
obscuredclarity.blogspot.com › 2011 › 07 › executing-shell-script-from-java.html
OBSCURED CLARITY: Executing a Shell Script from Java
July 28, 2011 - Run this command in the terminal to make it executable: sudo chmod 777 /temp/dump.sh Step 3: Now just run the script through the execShellCmd method. package mysql; import java.util.Date; import com.xeiam.utils.ExecUtils; /** * This class demonstrates running a shell script from within Java ...