一、cmd命令行运行
二、Java调用
utils工具类:CmdProcUtil.java
package com.huaqingth.safetyMonitor.utils;
import java.io.ByteArrayOutputStream;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class CmdProcUtil {
public static Log log = LogFactory.getLog(CmdProcUtil.class);
public static String MYSQL_NAME = "mysql";
public static void restartMySQL() {
if (SysTools.isWinSys()) {
proc("net stop " + MYSQL_NAME);
proc("net start " + MYSQL_NAME);
} else {
proc("service " + MYSQL_NAME + " restart");
}
}
public static boolean proc(String cmd) {
boolean flag = false;
Process pr = null;
try {
pr = Runtime.getRuntime().exec(cmd);
new PrintStream(pr.getInputStream()).start();
pr.waitFor();
//Thread.sleep(1000 * 10);
int exitValue = pr.exitValue();
flag = exitValue == 0;
} catch (Exception e) {
log.error("[CmdProcUtil::proc] " + e.getMessage());
}finally{
if(pr!= null)pr.destroy();
}
return flag;
}
public static boolean procByArgs(String[] args){
boolean flag = false;
Process pr = null;
try {
pr = Runtime.getRuntime().exec(args);
new PrintStream(pr.getInputStream()).start();
pr.waitFor();
//Thread.sleep(1000 * 10);
int exitValue = pr.exitValue();
flag = exitValue == 0;
} catch (Exception e) {
log.error("[CmdProcUtil::procByArgs] " + e.getMessage());
}finally{
if(pr!= null)pr.destroy();
}
return flag;
}
public static boolean CommPorcByArgs(String arg, String[] args){
boolean flag = true;
try{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
CommandLine commandline = new CommandLine(arg);
commandline.addArguments(args, false);
//ExecuteWatchdog watchDog = new ExecuteWatchdog(60*1000);
DefaultExecutor exec = new DefaultExecutor();
exec.setExitValues(null);
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream,errorStream);
exec.setStreamHandler(streamHandler);
//exec.setWatchdog(watchDog);
exec.execute(commandline);
/*String out = outputStream.toString("gbk");
String error = errorStream.toString("gbk");
System.out.println(out+error);*/
}catch(Exception e){
flag = false;
}
return flag;
}
public static boolean CommProc(String cmd){
boolean flag = false;
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
CommandLine commandline = CommandLine.parse(cmd);
//ExecuteWatchdog watchDog = new ExecuteWatchdog(60*1000);
DefaultExecutor exec = new DefaultExecutor();
exec.setExitValues(null);
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream,errorStream);
exec.setStreamHandler(streamHandler);
//exec.setWatchdog(watchDog);
exec.execute(commandline);
String out = outputStream.toString("gbk");
String error = errorStream.toString("gbk");
System.out.println(out+error);
} catch (Exception e) {
log.info("CommProc: "+e.getMessage());
}
return flag;
}
public static boolean CommProcNoOut(String cmd){
boolean flag = false;
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
CommandLine commandline = CommandLine.parse(cmd);
//ExecuteWatchdog watchDog = new ExecuteWatchdog(60*1000);
DefaultExecutor exec = new DefaultExecutor();
exec.setExitValues(null);
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream,errorStream);
exec.setStreamHandler(streamHandler);
//exec.setWatchdog(watchDog);
exec.execute(commandline);
} catch (Exception e) {
log.info("CommProcNoOut: "+e.getMessage());
}
return flag;
}
}
class PrintStream extends Thread {
java.io.InputStream __is = null;
public PrintStream(java.io.InputStream is) {
__is = is;
}
public void run() {
try {
while (this != null) {
int _ch = __is.read();
if (_ch != -1){
System.out.print((char) _ch);
}else{
break;
}
}
} catch (Exception e) {
CmdProcUtil.log.error(e.getMessage());
}
}
}
调用:
String bcompareDir = "E:/安装工具/Beyond_Compare/BeyondCompare4/";
//获取本地临时文件路径
String fileDir = FILEDIR+url.hashCode()+"/"+temperType+"/";
//输入命令行
String cmd = bcompareDir + "BComp.exe @" + bcompareDir + "test.txt "+fileDir+"old.txt "+fileDir+"new.txt "+fileDir+"diff.html";
CmdProcUtil.proc(cmd);
======================================================================
三、异常处理
1. 报错:
[CmdProcUtil::proc] Cannot run program "tesseract": CreateProcess error=14001, 应用程序无法启动,因为应用程序的并行配置不正确。有关详细信息,请参阅应用程序事件日志,或使用命令行 sxstrace.exe 工具。
解释:
程序调用BComp时报该错误,但在cmd中可调用,也可以直接打开BComp.exe
原因:
Windows Modules Installer服务被禁用;
解决办法:
开始 - 运行(输入services.msc)- 确定或回车,打开:服务(本地);
我们在服务(本地)窗口找到:Windows Modules Installer服务,查看是否被禁用;
如果Windows Modules Installer服务被禁用,我们必须把它更改为启用 - 手动。