How to execute batch (.bat) file from Java
Well as the title suggests, we will attempt to know how to execute bat file from Java. When you are a Java Developer, a lot is expected from you and you need to do all kind of jugglery with Java. Be it a very complex enterprise application cutting through all kind of integration technology or simple standalone program Java , developer has to try his hands at all places to achieve success.
I encountered a similar task and I had to try and integrate small web application with standalone legacy .bat files. “Yes” I was trying to execute .bat file from Java Web Application. In this article I will share the code which I have used. The flow was like, I click on a button and it will execute batch file. This approach was selected because both the applications were hosted on same server and the web application was used by only few well trained users.
Instead of using regular Runtime.getRuntime() I opted to use the Plexus Common Utilities. The pom entry for that you can refer here
1 2 3 4 5 |
<dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-utils</artifactId> <version>3.0.24</version> </dependency> |
And here is the complete program that execute bat file from Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
/** * */ package com.example; import java.io.File; import java.io.OutputStreamWriter; import org.codehaus.plexus.util.cli.CommandLineException; import org.codehaus.plexus.util.cli.CommandLineUtils; import org.codehaus.plexus.util.cli.Commandline; import org.codehaus.plexus.util.cli.WriterStreamConsumer; /** * @author pavan.solapure * */ public class BatRunner { public BatRunner() { String batfile = "run-java-program.bat"; String directory = "C:\\java-exec"; try { runProcess(batfile, directory); } catch (CommandLineException e) { e.printStackTrace(); } } public void runProcess(String batfile, String directory) throws CommandLineException { Commandline commandLine = new Commandline(); File executable = new File(directory + "/" +batfile); commandLine.setExecutable(executable.getAbsolutePath()); WriterStreamConsumer systemOut = new WriterStreamConsumer( new OutputStreamWriter(System.out)); WriterStreamConsumer systemErr = new WriterStreamConsumer( new OutputStreamWriter(System.out)); int returnCode = CommandLineUtils.executeCommandLine(commandLine, systemOut, systemErr); if (returnCode != 0) { System.out.println("Something Bad Happened!"); } else { System.out.println("Taaa!! ddaaaaa!!"); }; } /** * @param args */ public static void main(String[] args) { new BatRunner(); } } |
The plexus util has an option to provide the writer for bat program output and for error output. You can capture that in stream and do the processing. The utility also has an variant of method executeCommandLine which takes timeout as one of the parameter.
In above program you can see I am trying to run a batfile from given directory. I am simply preparing the complete path for bat file and providing to that plexus util method. Thats it, once you run it, the program will call desired bat file.
The above program calls a bat file “run-java-program.bat” and which in turn calls another Java program which generates sample file. After I run this, I can see a file is generated as shown below.
Please let me know if you see any issues in above example that tells you how execute bat file from Java.
Hi there, I’m having issues with the “returnCode” Command.
At least in debug at the point it reaches the “int returnCode” Line, the code stoppes working, the Batch File get’s executed but that’s it.
The Following If Statement also won’t be executed.
I actually need this as a pin pointer on a Gaming Server to start the Server. So I need a way to Exit the application, so the server won’t have 1000 of these instances running.
Hi actually I am trying to use this kind of logic to execute batch script(batch script having the micro focus excitable commands) and based on the batch script output java module has been to be raise exceptions.
Is it possible with this logic?
D:\eclipse-workspace\tetsttemp>javac -cp lib/* Runtime/* Test/* Tools/*
javac: invalid flag: Runtime/*
Usage: javac
use -help for a list of possible options
D:\eclipse-workspace\tetsttemp>java -cp .;lib/* Test.Runer
Error: Could not find or load main class Test.Runer
Something Bad Happened!
can you help me???
Can it be used if the (.bat) file is in another remote folder.
Basically I want to start/stop the tomcat server by using this logic(I have the IP address and tomcat port access)
Hi Harsha,
If the machine you are running from has access to remote server and bat file then I think you can use it.
hi i have issue when i am trying the the above example execution of batch file throwing error as access denied
Hi Karan,
Can you check if you have admin access and you are running the program/bat file with Administrator mode or access.