Java 这个程序=程序; this.sourceFolder=sourceFolder; } @凌驾 受保护的无效进程(列表块){ //回到EDT for(字符串值:块){ if(value.equalsIgnoreCase(“处理”)){ lblMessage.setText(“处理”); ImageIcon图标=新的ImageIcon(“clock.gif”); lblPic.setIcon(图标); }else if(value.equalsIgnoreCase(“完成”)){ lblMessage.setText(“完成”); }否则{ //可能还有其他消息。。。 } } } @凌驾 受保护的整数doInBackground()引发异常{ int结果=-1; 字符串strExecutableFilename=程序; 字符串strSourceFolder=sourceFolder; 字符串strCommand=strExecutableFilename+“”+strSourceFolder; 发布(“处理”); //lblMessage.setText(“处理”); //ImageIcon图标=新的ImageIcon(“clock.gif”); //lblPic.setIcon(图标); 试一试{ ProcessBuilder pb=新的ProcessBuilder(程序); 重定向错误(); pb.directory(新文件(strSourceFolder)); Process procCommand=pb.start(); //Process procommand=Runtime.getRuntime().exec(strCommand); 试一试{ 结果=procommand.waitFor(); }捕获(中断异常异常){ 异常。printStackTrace(); }最后{ } //lblMessage.setText(“完成”); 出版(“完成”); }捕获(IOE异常){ e、 printStackTrace(); } 返回结果; } }

Java 这个程序=程序; this.sourceFolder=sourceFolder; } @凌驾 受保护的无效进程(列表块){ //回到EDT for(字符串值:块){ if(value.equalsIgnoreCase(“处理”)){ lblMessage.setText(“处理”); ImageIcon图标=新的ImageIcon(“clock.gif”); lblPic.setIcon(图标); }else if(value.equalsIgnoreCase(“完成”)){ lblMessage.setText(“完成”); }否则{ //可能还有其他消息。。。 } } } @凌驾 受保护的整数doInBackground()引发异常{ int结果=-1; 字符串strExecutableFilename=程序; 字符串strSourceFolder=sourceFolder; 字符串strCommand=strExecutableFilename+“”+strSourceFolder; 发布(“处理”); //lblMessage.setText(“处理”); //ImageIcon图标=新的ImageIcon(“clock.gif”); //lblPic.setIcon(图标); 试一试{ ProcessBuilder pb=新的ProcessBuilder(程序); 重定向错误(); pb.directory(新文件(strSourceFolder)); Process procCommand=pb.start(); //Process procommand=Runtime.getRuntime().exec(strCommand); 试一试{ 结果=procommand.waitFor(); }捕获(中断异常异常){ 异常。printStackTrace(); }最后{ } //lblMessage.setText(“完成”); 出版(“完成”); }捕获(IOE异常){ e、 printStackTrace(); } 返回结果; } },java,swing,process,Java,Swing,Process,您还应该熟悉ProcessBuilder。它有许多用于构建流程的有用方法,并克服了人们在尝试让Runtime.getRuntime().exec工作时遇到的一些困难 您应该查看更多详细信息确认onLaunchProgram是在EDT上下文之外执行的,否则这没有什么区别。Jean,谢谢您的回复,但我使用了您的代码,我仍然有相同的问题。它运行exe程序,然后在exe程序完成后显示时钟和消息。请验证onLaunchProgram是否在EDT上下文之外执行,否则这不会有任何影响。Jean,感谢您的回复,

您还应该熟悉
ProcessBuilder
。它有许多用于构建流程的有用方法,并克服了人们在尝试让
Runtime.getRuntime().exec
工作时遇到的一些困难


您应该查看更多详细信息

确认onLaunchProgram是在EDT上下文之外执行的,否则这没有什么区别。Jean,谢谢您的回复,但我使用了您的代码,我仍然有相同的问题。它运行exe程序,然后在exe程序完成后显示时钟和消息。请验证onLaunchProgram是否在EDT上下文之外执行,否则这不会有任何影响。Jean,感谢您的回复,但我使用了您的代码,我仍然有相同的问题。它运行exe程序,然后在exe程序完成后显示时钟和消息。

    ...

    JTextField txtFolder = new JTextField();
    JLabel lblMessage = new JLabel();
    JLabel lblPic = new JLabel();
    JButton btnLaunchApplication = new JButton("Launch Program");  

    ...  

    btnLaunchApplication.addActionListener(new ActionListener() {  
        public void actionPerformed(ActionEvent evt) {  
            onLaunchProgram(evt);  
        }  
     });  

    ...

    if (returnVal == JFileChooser.APPROVE_OPTION){
        file = fc.getSelectedFile();
        txtFolder.setText(file.getAbsolutePath());
    }

    ...

    private void onLaunchProgram(ActionEvent evt) {
        String strExecutableFilename = "MyExecutableProgam";
        String strSourceFolder = txtFolder.getText();
        String strCommand = strExecutableFilename + " " + strSourceFolder;
        lblMessage.setText("Processing");
        ImageIcon icon = new ImageIcon("clock.gif");
        lblPic.setIcon(icon);
        try {
            Process procCommand = Runtime.getRuntime().exec(strCommand);
            try {
                procCommand.waitFor();
            } catch (InterruptedException exception) {
                exception.printStackTrace();
            } finally {
            }
            lblMessage.setText("Finished");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }
private void onLaunchProgram(ActionEvent evt) {
        String strExecutableFilename = "MyExecutableProgam";
        String strSourceFolder = txtFolder.getText();
        String strCommand = strExecutableFilename + " " + strSourceFolder;
        ImageIcon icon = new ImageIcon("clock.gif");
        javax.swing.SwingUtilities.invokeLater(
            new Runnable() {
                 public void run() {
                     lblMessage.setText("Processing");
                     lblPic.setIcon(icon);
                 }
            });

        try {
            Process procCommand = Runtime.getRuntime().exec(strCommand);
            try {
                procCommand.waitFor();
            } catch (InterruptedException exception) {
                exception.printStackTrace();
            } finally {
            }

            javax.swing.SwingUtilities.invokeLater(
                new Runnable() {
                    public void run() {
                      lblMessage.setText("Finished");
                    }
                 });
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }
public class ProcessWorker extends SwingWorker<Integer, String> {

    private String program;
    private String sourceFolder;

    public ProcessWorker(String program, String sourceFolder) {
        this.program = program;
        this.sourceFolder = sourceFolder;
    }

    @Override
    protected void process(List<String> chunks) {
        // Back on the EDT
        for (String value : chunks) {
            if (value.equalsIgnoreCase("PROCESSING")) {
                lblMessage.setText("Processing");
                ImageIcon icon = new ImageIcon("clock.gif");
                lblPic.setIcon(icon);
            } else if (value.equalsIgnoreCase("FINISHED")) {
                lblMessage.setText("Finished");
            } else {
                // Possible some other message...
            }
        }
    }

    @Override
    protected Integer doInBackground() throws Exception {
        int result = -1;

        String strExecutableFilename = program;
        String strSourceFolder = sourceFolder;
        String strCommand = strExecutableFilename + " " + strSourceFolder;
        publish("PROCESSING");
//        lblMessage.setText("Processing");
//        ImageIcon icon = new ImageIcon("clock.gif");
//        lblPic.setIcon(icon);
        try {
            ProcessBuilder pb = new ProcessBuilder(program);
            pb.redirectError();
            pb.directory(new File(strSourceFolder));
            Process procCommand = pb.start();
//            Process procCommand = Runtime.getRuntime().exec(strCommand);
            try {
                result = procCommand.waitFor();
            } catch (InterruptedException exception) {
                exception.printStackTrace();
            } finally {
            }
//            lblMessage.setText("Finished");
            publish("FINISHED");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }
}