无法在Process.Runtime.exec语句行之前执行任何操作

时间:2011-03-31 19:02:59

标签: java process runtime.exec

经过长时间的搜索后,我的第一篇文章在此处得到了答案,但在这个问题上得到了答案,请在这个问题上帮助我。

我正在使用Netbean 6.9.1来构建一个Java应用程序,它大量调用几个不同的外部程序,因此我使用了进程和运行时函数来调用外部程序。

整个申请流程分为几个阶段,我希望通过更新GUI textarea告知用户应用程序当前运行的阶段,代码如下所示:

public void executeCommand(String cmd,File path)     {

    try
    {
        ****areaOutput.setText("Executing audio decoding, please wait till process is done\n");****  
        btnTranscribe.setEnabled(false);
        areaOutput.setEditable(false);
        areaOutput.setEnabled(false);
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(cmd , null, path);
        InputStream stderr = proc.getErrorStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        System.out.println("<ERROR>");
        while ( (line = br.readLine()) != null)
            System.out.println(line);
        System.out.println("</ERROR>");
        int exitVal = proc.waitFor();
        System.out.println("Process exitValue: " + exitVal);
        areaOutput.append("\n\nConversion is done, processing with features extraction....");
    } catch (Throwable t)
      {
        t.printStackTrace();
      }

}

如上面的代码所示,我希望在执行命令之前设置Textarea并禁用一些按钮,但是当应用程序运行时,所有这些行似乎无法工作,并且在应用程序本身没有任何更改,直到命令完成执行后,任何运行预命令代码的解决方案都会在.exec()开始运行之前先执行?

感谢您就此问题提供的大力帮助和建议。

祝你好运, Striky

P / S:

在那里,我为这个CmdExec创建了一个Thread类,以便在不同的线程中执行cmd:

     public class CmdExec extends Thread
     {
 private String cmd;
     private File path;

      public CmdExec() {
      }

      public CmdExec(String cmd, File path) {
  this.cmd = cmd;
  this.path = path;
      }

      public void run(){

  try
    {
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(cmd , null, path);
        InputStream stderr = proc.getErrorStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        System.out.println("<ERROR>");
        while ( (line = br.readLine()) != null)
            System.out.println(line);
        System.out.println("</ERROR>");
        int exitVal = proc.waitFor();
        System.out.println("Process exitValue: " + exitVal);
    } catch (Throwable t)
      {
        t.printStackTrace();
      }

}     }

并且为了调用这个类,

CmdExec tryDemo = new CmdExec();         tryDemo = new CmdExec(strSegment,fSegment);         tryDemo.run(); 用于启动线程,但我没有把SwingUtilities.invokeLater放在这些进程的任何部分,它只是不会运行tryDemo.run(),因为它是无效的......

另外,我可以知道到目前为止我做得对吗?非常感谢您对此问题的帮助

P / S 2:我刚刚为GUI更新命令添加了另一个可运行的代码(用于进程执行的线程,可运行到GUI更新),如下所示:

            Runnable doWorkRunnable = new Runnable() {
            public void run() {
    System.out.println("hello world");
    btnTranscribe.setEnabled(false);
    areaOutput.setEditable(false);
    areaOutput.setEnabled(false);
    areaOutput.setText("Performing segmentation, please wait till process is done\n"); }
};

我在执行流程之前使用了SwingUtilies.invokeLater,如下所示:

        SwingUtilities.invokeLater(doWorkRunnable);
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(cmd , null, path);

但是所有这些都失败了,我是否得到了错误的GUI序列并处理线程协调?

2 个答案:

答案 0 :(得分:2)

您正在EDT(更新gui的线程)上执行此工作。所以,gui无法更新,直到所有这些工作完成。你想要做的是运行一个单独的线程来完成所有的工作,并定期调用带有状态更新的SwingUtilities.invokeLater。

答案 1 :(得分:0)

尝试在执行方法之前放入睡眠。验证发生了什么。

相关问题