package com.lx.product.seal.utils;
|
|
import com.lx.product.seal.log.Logger;
|
|
import java.io.BufferedReader;
|
import java.io.ByteArrayOutputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.InputStreamReader;
|
import java.io.OutputStreamWriter;
|
import java.io.PrintStream;
|
import java.io.PrintWriter;
|
import java.util.concurrent.Callable;
|
import java.util.concurrent.Future;
|
|
import javax.swing.JLabel;
|
|
/**
|
* Copyright © 6c
|
* @Date: 2021-02-23 15:24
|
* @Author: 6c
|
* @Description:
|
*/
|
public class CommonUtil {
|
private static Logger log = Logger.getInstance();
|
|
/**
|
* 获取异常信息
|
*
|
* @param e 异常
|
* @return e.info
|
*/
|
public static String getExceptionInfo(Exception e) {
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
e.printStackTrace(new PrintStream(outputStream));
|
String exception = outputStream.toString();
|
try {
|
outputStream.close();
|
} catch (IOException e1) {
|
log.writeInfo("获取日志信息出错," + e1.getMessage(), Logger.ERROR_TYPE);
|
}
|
return exception;
|
}
|
|
/**
|
* 执行批处理文件且关闭窗口
|
*/
|
public static void runbatAndCloseIframe(String path) {
|
try {
|
String cmd = "cmd /k start " + path;
|
log.writeInfo("正在执行批处理文件: " + path, Logger.INFO_TYPE);
|
Runtime.getRuntime().exec(cmd);
|
} catch (IOException e) {
|
log.writeInfo("执行批处理文件出错,路径为: " + path + e.getMessage(), Logger.ERROR_TYPE);
|
}
|
}
|
/**
|
* 初始数据
|
*
|
* @param cmdDos
|
* @throws Exception
|
*/
|
static public void run(String cmdDos,JLabel web) throws Exception {
|
ProcessBuilder pb = new ProcessBuilder().command("cmd.exe", "/c", cmdDos).inheritIO();
|
pb.redirectErrorStream(true);//这里是把控制台中的红字变成了黑字,用通常的方法其实获取不到,控制台的结果是pb.start()方法内部输出的。
|
System.out.println("编译开始..............");
|
//pb.start().waitFor();//等待语句执行完成,否则可能会读不到结果。
|
Process process = pb.start();
|
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
String line;
|
while ((line = br.readLine()) != null) {
|
System.out.println(line);
|
}
|
int exitCode = process.waitFor();
|
// 监听成功
|
System.out.println("exitCode = "+exitCode);
|
System.out.println("编译结束..............");
|
}
|
|
}
|