Monday, June 18, 2012

Executing a Shell Script From a java Class

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 = "./";
           ProcessBuilder processBuilder = new ProcessBuilder(shellScript, command.getOperation(), "argument-one");

        Process process = processBuilder.start();

        try {
            int shellExitStatus = process.waitFor();
            if (shellExitStatus != 0) {
      "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.


  1. Hi Eshan,

    how do i call a .sh file from java program.

    reply me asap.


  2. Hi Siva,
    As you can see here I have executes a sh file. ("./").
    For the bat file also you can use the same way. :)

  3. Hi,
    I'm executing ProcessBuilder processBuilder = new ProcessBuilder("./"); file is residing in the same directory from where i'm calling but its giving me below error.
    Exception in thread "main" CreateProcess: .\ error=193

  4. HI Eshan

    how do i call the same ./ from mac terminal using java eclipse


  5. HOw to exceute a shell script from remote unix machine using java?

  6. i tried this.
    What is Operaction command?
    could you pls share the whole code

  7. Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
    Best Devops Training in pune
    Best Devops Training institute in Chennai

  8. 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.
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

  9. 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.
    python Training in Pune
    python Training in Chennai
    python Training in Bangalore