shichongfu
2023-02-14 aba34133ba4635b7a7c83879550e4d8d98664530
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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("编译结束..............");
    }
    
}