Java代码实现运行cmd命令 命令工厂:
public class CommandFactory {
public static CommandLine createCommand(String command ,String[] parameter){
if(null == command || "".equals(command)){
System.out.println("the command must not null!") ; return null ; } return createJavaCommand(command, parameter) ; }
private static CommandLine createJavaCommand(String command ,String[] parameter){
//final String SPLIT = " " ; if(null == parameter || parameter.length < 1){
return new CommandLine(command) ; } else {
return new CommandLine(command,parameter); } } } 命令处理器:
public class CommandHandler {
public static void excute(CommandLine command) throws IOException, InterruptedException {
if (null == command || "".equals(command)) {
System.out.println("the parameter[command] must not null!"); return; } try {
System.out.println("execute command start:" + command) ; Runtime runtime = Runtime.getRuntime(); Process pro = runtime.exec(command.toString()); SimpleThreadPool queue = SimpleThreadPool.getWorkQueue(4) ; CommandStream commandStream = new CommandStream(); commandStream.setCharset("gbk") ; commandStream.setCommandLine(command) ; commandStream.setIs(pro.getInputStream()) ; comma
|