Java进程线程问题

Java进程线程问题,java,multithreading,swing,process,callback,Java,Multithreading,Swing,Process,Callback,我的问题涉及到进程和运行时,并试图在允许主Java Swing GUI更新状态栏的同时运行它,因此: 在过去的三个月里,我一直在为我的软件开发小组开发一个内部开发工具。最基本的是,它接受GUI根据用户输入定义的一些变量。然后,它将这些文件编译成批处理文件,供命令行工具进行解释。在点击“补丁”按钮后让GUI运行命令行是相当有趣的,但我无法读入执行过程中可能出现的任何错误,这在很大程度上是因为我在runtimeVar.exec()的参数中使用了“start”。我可以让一个常驻Java的家伙来帮我,但

我的问题涉及到进程和运行时,并试图在允许主Java Swing GUI更新状态栏的同时运行它,因此:

在过去的三个月里,我一直在为我的软件开发小组开发一个内部开发工具。最基本的是,它接受GUI根据用户输入定义的一些变量。然后,它将这些文件编译成批处理文件,供命令行工具进行解释。在点击“补丁”按钮后让GUI运行命令行是相当有趣的,但我无法读入执行过程中可能出现的任何错误,这在很大程度上是因为我在runtimeVar.exec()的参数中使用了“start”。我可以让一个常驻Java的家伙来帮我,但它从将进程分叉到另一个线程,切换到在同一个线程中运行进程,阻止GUI响应任何其他内容。虽然这个工具运行得很好,但测试人员却在抱怨,因为他们不知道它是否正常工作

长话短说:我需要将runtimeVar.exec()分叉到另一个进程以允许状态栏更新,但这样做需要一些重大的返工,因为我的代码目前看起来是这样的:

    private void runPatchTool(File directory, String batchName) throws Exception{
    /*************************************************************************** 
     * Method: runPatchTool
     * Tab: Both
     * Status: Completed, as of 1/10/12
     * 
     * This method will read in a directory and batch file name and output
     * these commands to the command line, running the batch file while reading
     * in the output via the the InputStreamReader and parsing this to check
     * for any errors that may have occurred during the patching.
     * 
     **************************************************************************/
    String tempOFP = null;
    Boolean labType = false;
    String tmpCountry = null;

    int yesNo;
    String s = " ";
    String er = " ";
    String[] query = new String[]{"cmd.exe","/c",directory.getPath() + "\\" + batchName};
//    displayStatus("Running Patch_tool.exe . . . ");    
    ProcessBuilder pb = new ProcessBuilder(query);
    pb.directory(directory);
    try{
        Process proc = pb.start();
        InputStreamReader inStrmRd = new InputStreamReader(proc.getInputStream());
        InputStreamReader erStrmRd = new InputStreamReader(proc.getErrorStream());
        BufferedReader commandResult = new BufferedReader(inStrmRd);
        BufferedReader errResult = new BufferedReader(erStrmRd);
        String line = " ";
        String erLine = " ";
        try {
            while ((line = commandResult.readLine()) != null) {
                s += line + "\n";
            }
            while((erLine = errResult.readLine()) != null){
                er += erLine + "\n";
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this,"Path Tool Error: " + e, "ERROR", JOptionPane.ERROR_MESSAGE);
        }
//        readOutputFiles();
    } catch(IOException e){
        JOptionPane.showMessageDialog(this,"Path Tool Error: " + e, "ERROR", JOptionPane.ERROR_MESSAGE);
    }
    StringBuffer erMsg = new StringBuffer("at least one assembler error has occurred");
    CharSequence erMsgSeq = erMsg;
    try{
        PrintWriter outputFile = new PrintWriter(new FileOutputStream(directory.getPath() + "\\cmdout.txt"));
        outputFile.print(s);
        outputFile.close();
        PrintWriter erFile = new PrintWriter(new FileOutputStream(directory.getPath() + "\\errout.txt"));
        erFile.print(er);
        erFile.close();
    } catch(Exception ex){
        System.out.println("Something happened in PrintWriter: " + ex);
    }
    }
我省略了更多的代码,但它与手头的具体问题无关


好的,说到这里,有没有一种方法可以在不阻塞GUI的情况下运行这个进程,同时等待进程结束(使用回调或其他方法)在执行下一步之前?

这就是SwingWorker的作用:@a_horse_,没有名字hmmm,我对SwingWorker和
Process/processbuilder
有非常错误的经验,因为我的工作没有任何缺少
执行器
定时器
可运行线程
,请阅读没有名字的@a_horse_工作,但(可能是我头脑中的真空)我无法加载进程。getErrorStream()正确,我经常模拟…,好吧,请结束这场辩论:-)@mKorbel:进程问题。getErrorStream()与在Swing GUI中使用SwingWorker无关好吧,我正在阅读SwingWorker,但我想知道ProcessBuilder是否也能工作,per@mKorbel。只要运行流程并等待它完成,而不阻塞GUI,任何操作都可以正常工作。(也许我应该把这个放在我原来的问题>。