Plexus utils提供了一个Commandline类型,可以调用任意命令行并处理输出的解析。
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
}