AdBlock Detected

It looks like you're using an ad-blocker!

Our team work realy hard to produce quality content on this website and we noticed you have ad-blocking enabled. Advertisements and advertising enable us to continue working and provide high-quality content.

How to execute scripts in Java

In this new Refactorizando post, we are going to see how to execute scripts in Java of any type, as a continuation of the previous article about executing SQL with Flyway.

Many times, we come across an sh file or script that we need to execute, for example, a file with multiple curls. In these cases, it’s better to execute the file directly rather than having to perform some transformation in Java. If, instead of directly executing it, we had to transform each request with a REST client, it would be very costly.

So, let’s see with an example how we can execute any script in our Java application using Runtime and ProcessBuilder. Once executed, we’ll be able to capture the response for both errors and input to see the result.

Executing Script in Java with Runtime

One of the ways we can execute scripts in Java is by using the following command:

Runtime.getRuntime().exec(command);

In the command, we’ll pass the command to execute. For example, if we had to execute an sh script:

String path = "/home/refactorizando/script.sh"
String[] command = {"sh",path}
Process process = Runtime.getRuntime().exec(command);

Once we’ve finished the process, we can use process.destroy().

Executing Script in Java with ProcessBuilder

Another equally easy and straightforward way to execute a script in Java is by using ProcessBuilder.

ProcessBuilder processBuilder = new ProcessBuilder()
processBuilder.directory(new File("/home/refactorizando/script.sh"));
Process process = processBuilder.start();



Once we’ve finished the process, we can obtain the exit code:

int exitCode = process.exitValue();

and end the process with:

process.destroy();

Viewing Output when Running a Script in Java

After we’ve executed the script, we can use process.getInputStream() and process.getErrorStream(). The latter is for viewing errors and seeing the execution result as shown below:

javaCopy code

   String commandRead;
 
   BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
    
    while ((commandRead = stdInput.readLine()) != null) {

      log.info(commandRead);
    }

    BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

    while ((commandRead = stdError.readLine()) != null) {

      log.info(commandRead);
    }

Conclusion

In this post, we’ve seen how to execute scripts in Java in two different ways, both very simple and requiring very few lines of code. This undoubtedly saves time in your Java programs.

If you need more information, you can leave us a comment or send an email to refactorizando.web@gmail.com You can also contact us through our social media channels on Facebook or twitter and we will be happy to assist you!!

Leave a Reply

Your email address will not be published. Required fields are marked *