I want to execute a batch file from a java program.
I am using the following command.
Runtime.getRuntime().exec(\"server.bat\");
Bu
Plexus utils provides a Commandline type that can invoke an arbitrary command line and handle parsing of the output.
Commandline cl = new Commandline();
cl.setExecutable( "cmd.exe" );
cl.createArg().setValue( "/c" );
cl.setWorkingDirectory( new File(System.getProperty("user.dir"),
"/com/project/util/Server.bat"));
cl.createArg().setValue( "/c" );
StreamConsumer consumer = new StreamConsumer() {
public void consumeLine( String line ) {
//do something with the line
}
};
StreamConsumer stderr = new StreamConsumer() {
public void consumeLine( String line ) {
//do something with the line
}
};
int exitCode;
try {
exitCode = CommandLineUtils.execute( cl, consumer, stderr, getLogger() );
} catch ( CommandLineException ex ) {
//handle exception
}