In this post I am going to explain the way of executing a shell script through a java class. But this kind of implementations are discourage by java programming language since this will remove the great power of java which is portability. (compile one one machine and can run on anywhere we have JRE).
For this purpose we can use java ProcessBuilder and execute shell script file with the help of it.
private static void executeProcess(Operation command, String database) throws IOException,
InterruptedException {
final File executorDirectory = new File("src/main/resources/");
private final static String shellScript = "./sample.sh";
ProcessBuilder processBuilder = new ProcessBuilder(shellScript, command.getOperation(), "argument-one");
processBuilder.directory(executorDirectory);
Process process = processBuilder.start();
try {
int shellExitStatus = process.waitFor();
if (shellExitStatus != 0) {
logger.info("Successfully executed the shell script");
}
} catch (InterruptedException ex) {
logger.error("Shell Script preocess is interrupted");
}
}
In this case my shell script is reside in resources folder. You can see I have set the process builder directory to that, so the commands are executing from that directory.And also we can pass any number of arguments to shell script via process builder.In this case we have pass a single parameter to the shell script and from the shell script it can be accessed by $1.
Following is the sample shell script we have invoked through the java class.
echo "Sample script is executing"
echo "parameter is :" $1
Same way you can invoke a windows batch file as well.
Hi Eshan,
ReplyDeletehow do i call a .sh file from java program.
reply me asap.
Thanks&Regards,
siva
Hi Siva,
ReplyDeleteAs you can see here I have executes a sh file. ("./sample.sh").
For the bat file also you can use the same way. :)
Hi,
ReplyDeleteI'm executing ProcessBuilder processBuilder = new ProcessBuilder("./Test.sh");
Test.sh file is residing in the same directory from where i'm calling but its giving me below error.
Exception in thread "main" java.io.IOException: CreateProcess: .\Test.sh error=193
HI Eshan
ReplyDeletehow do i call the same ./Test.sh from mac terminal using java eclipse
Regards,
Harsh
HOw to exceute a shell script from remote unix machine using java?
ReplyDeletei tried this.
ReplyDeleteWhat is Operaction command?
could you pls share the whole code
I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
ReplyDeleteSelenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
You got an extremely helpful website I actually have been here reading for regarding an hour. I’m an initiate and your success is incredibly a lot of a concept on behalf of me.
ReplyDeletepython Training in Pune
python Training in Chennai
python Training in Bangalore
ReplyDeleteThank you for this blog. This for very interesting and useful. DevOps Training | Certification in Chennai | DevOps Training | Certification in anna nagar | DevOps Training | Certification in omr | DevOps Training | Certification in porur | DevOps Training | Certification in tambaram | DevOps Training | Certification in velachery
Excellent, Thanks for sharing the valuable information. it’s really helpful to us and this is very interesting. Keep sharing on updated tutorials…
ReplyDeleteSalesforce Training | Online Course | Certification in chennai | Salesforce Training | Online Course | Certification in bangalore | Salesforce Training | Online Course | Certification in hyderabad | Salesforce Training | Online Course | Certification in pune
kralbet
ReplyDeletebetpark
tipobet
slot siteleri
kibris bahis siteleri
poker siteleri
bonus veren siteler
mobil ödeme bahis
betmatik
Q5Q0MB
شركة مكافحة النمل الابيض بالخبر cHa3XM2EZl
ReplyDeleteCool and that i have a nifty present: Where Is Charlotte Church House Renovation home renovation contract
ReplyDelete