(String command, JTextArea resultArea, JTabbedPane tabbedPane, String tabName, String pluginJsonPath)
| 555 | } |
| 556 | |
| 557 | static void executeCommand(String command, JTextArea resultArea, JTabbedPane tabbedPane, String tabName, String pluginJsonPath) { |
| 558 | try { |
| 559 | ProcessBuilder builder = new ProcessBuilder(command.split("\\s+")); |
| 560 | builder.redirectErrorStream(true); |
| 561 | runningProcess = builder.start(); |
| 562 | runningProcess.getOutputStream().close(); |
| 563 | |
| 564 | Thread outputThread = new Thread(() -> { |
| 565 | Process currentProcess = runningProcess; // 创建Process的局部副本 |
| 566 | try (BufferedReader reader = new BufferedReader( |
| 567 | new InputStreamReader(currentProcess.getInputStream(), StandardCharsets.UTF_8))) { |
| 568 | String line; |
| 569 | while ((line = reader.readLine()) != null) { |
| 570 | final String lineCopy = line; |
| 571 | SwingUtilities.invokeLater(() -> { |
| 572 | resultArea.append(lineCopy + "\n"); |
| 573 | }); |
| 574 | } |
| 575 | } catch (IOException e) { |
| 576 | SwingUtilities.invokeLater(() -> { |
| 577 | resultArea.append("Error reading output: " + e.getMessage() + "\n"); |
| 578 | }); |
| 579 | } finally { |
| 580 | try { |
| 581 | currentProcess.waitFor(); // 使用局部副本而不是runningProcess |
| 582 | } catch (InterruptedException ex) { |
| 583 | SwingUtilities.invokeLater(() -> { |
| 584 | resultArea.append("Process was interrupted: " + ex.getMessage() + "\n"); |
| 585 | }); |
| 586 | } |
| 587 | SwingUtilities.invokeLater(() -> { |
| 588 | if (wasManuallyStopped.get()) { |
| 589 | resultArea.append("\n程序被手动停止\n"); |
| 590 | } else { |
| 591 | resultArea.append("\n程序运行结束\n"); |
| 592 | addPluginTab(tabbedPane, tabName, pluginJsonPath); // 新增标签 |
| 593 | switchTab(tabbedPane, tabName); |
| 594 | } |
| 595 | wasManuallyStopped.set(false); // 重置状态 |
| 596 | }); |
| 597 | } |
| 598 | }); |
| 599 | |
| 600 | outputThread.start(); |
| 601 | |
| 602 | } catch (Exception ex) { |
| 603 | resultArea.setText("Error executing command: " + ex.getMessage()); |
| 604 | JOptionPane.showMessageDialog(null, "Execution failed", "Error", JOptionPane.ERROR_MESSAGE); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | public static void main(String[] args) { |
| 609 | } |
no test coverage detected