首页 > 技术文章 > JSON 级 Process

aloneblog 2018-08-08 01:54 原文

 1     @Override
 2     public void run() {
 3         StatusPanel.isRunning = true;
 4         this.setName("ExecuteThread");
 5 
 6         String exe = "python";
 7         String command = "C:\\Users\\Administrator\\Desktop\\nfv.py";
 8         String num1 = StatusPanel.targetTextField.getText();
 9         String[] cmdArr = new String[] {exe,command,num1};
10         logger.info(exe+" "+command+" "+num1);
11         JProgressBar progressBar = StatusPanel.progressTotal;
12         Process process = null;
13         InputStream in=null;
14         try {
15             int rs = 0;
16             progressBar.setValue(0);
17             ProgressThread progressThread = new ProgressThread();
18             progressThread.start();
19             ProcessBuilder builder = new ProcessBuilder("python","C:\\Users\\Administrator\\Desktop\\nfv.py");
20             builder.redirectErrorStream(true);
21             process = builder.start();
22             in = process.getInputStream();
23             BufferedReader br = new BufferedReader(new InputStreamReader(in));
24             String output = null;
25 
26             while (null != (output = br.readLine()))
27             {
28                logger.info(output);
29             }
30             rs = process.waitFor();
31            logger.info(String.valueOf(rs));
32 
33 
34             progressBar.setValue(100);
35             JOptionPane.showMessageDialog(null,"转换完成","信息",JOptionPane.INFORMATION_MESSAGE);
36         } catch (Exception e) {
37             progressBar.setValue(0);
38             JOptionPane.showMessageDialog(null,"转换失败","信息",JOptionPane.ERROR_MESSAGE);
39             logger.error(Utils.getStackMsg(e));
40         }finally {
41             StatusPanel.buttonStartNow.setEnabled(true);
42             StatusPanel.isRunning = false;
43             try {
44                 in.close();
45             } catch (IOException e) {
46                 e.printStackTrace();
47             }
48         }
49     }
 1     @Override
 2     public void run(){
 3         this.setName("ProgressThread");
 4         JProgressBar progressBar = StatusPanel.progressTotal;
 5         while (progressBar.getValue() != 100) {
 6             try {
 7                 JSONObject jsonTask =new JSONObject(Utils.readFile("C:\\Users\\Administrator\\Desktop\\task.json"));
 8                 JSONObject task = jsonTask.getJSONObject("default");
 9                 int progress = task.getInt("progress");
10                 if (progress> progressBar.getValue()){
11                     progressBar.setValue(progress);
12                 }
13                 Thread.sleep(1000);
14             } catch (Exception e) {
15 //               logger.error(Utils.getStackMsg(e));
16                 e.printStackTrace();
17             }
18 
19         }
20     }

 

推荐阅读