command = "cmd.exe /c "+command;
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
PumpStreamHandler psh = new PumpStreamHandler(stdout);
CommandLine cl = CommandLine.parse(command);
DefaultExecutor exec = new DefaultExecutor();
exec.setStreamHandler(psh);
try{
exec.execute(cl);
}catch (Exception e){
e.printStackTrace();
}
return stdout.toString();
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>1.3</version>
</dependency>
使用Apache Common-Exec 框架