How to run Unix shell script from Java code

0 votes

It is quite simple to run a Unix command from Java.

Runtime.getRuntime().exec(myCommand);

But is it possible to run a Unix shell script from Java code? If yes, would it be a good practice to run a shell script from within Java code?

Oct 26, 2018 in Java by Sushmita
• 6,910 points
6,214 views

1 answer to this question.

0 votes
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();
answered Oct 26, 2018 by Daisy
• 8,120 points

Related Questions In Java

0 votes
1 answer

Is it possible to run a java program from command line on windows?How?

  Let's say your file is in C:\myprogram\ Run ...READ MORE

answered Apr 18, 2018 in Java by sophia
• 1,400 points
2,383 views
0 votes
0 answers

How to run python script in java

Aug 22, 2019 in Java by anonymous

closed Aug 22, 2019 by Omkar 3,498 views
+1 vote
1 answer

How to get Alias name of AWS Lambda function from java code?

Hey @Abhishek, you could probably use the ...READ MORE

answered Apr 1, 2020 in Java by Sirajul
• 59,230 points
1,780 views
0 votes
1 answer

How to run a Java program from the command line on Windows?

Hello @kartik, In case your Java class is ...READ MORE

answered Apr 9, 2020 in Java by Niroj
• 82,880 points
688 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

To run a shell script on a Unix console or Mac terminal

To run a non-executable sh script, use: sh myscript To run ...READ MORE

answered May 16, 2019 in Linux Administration by Upasana
• 8,620 points
618 views
0 votes
1 answer

How can I find out which files differ in two directory trees?

Use this; diff -qr dir1/ dir2/ READ MORE

answered Jun 13, 2019 in Linux Administration by Upasana
• 8,620 points
575 views
0 votes
1 answer

In Java, how to find out the min/max values from array of primitive data types?

import java.util.Arrays; import java.util.Collections; import org.apache.commons.lang.ArrayUtils; public class MinMaxValue { ...READ MORE

answered Jun 12, 2018 in Java by Daisy
• 8,120 points
1,427 views
0 votes
5 answers

How to compare Strings in Java?

String fooString1 = new String("foo"); String fooString2 = ...READ MORE

answered Jul 12, 2018 in Java by Daisy
• 8,120 points
2,186 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP