Wednesday, October 17, 2012

How do I run a batch file from my Java Application?

Sometimes we want to run a batch file from our Java Application. This batch could be either a .bat file or a .sh file, it depends on which operating system are we using.

After some research ... I created a class that do the job.

Basically, we have 4 parameters.

  • programAbsolutePath: Where is the batch file to execute?
  • parameters: Which are the parameters of the batch file?
  • environmentVariables: If you want to override some environment variables
  • baseDirectory: Which will be the base directory of the batch?. This could be usefull when the batch file use relative paths in its process.

This class only executes batches for unix/windows. If you want to use it in other Operating Systems you should specify which are the execution path (like EXEC_WINDOWS and EXEC_UNIX constants)

That's all.

No comments:

Post a Comment