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

发布于 2024-10-28 05:44:03 字数 3395 浏览 0 评论 0原文

经过长时间的搜索,我在这里发表了第一篇文章,但尚未得到有关此问题的答案,请帮助我解决此问题。

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

整个应用程序过程分为几个阶段,我希望通过更新GUI文本区域来通知用户应用程序当前运行到哪个阶段,代码如下所示:

public voidexecuteCommand(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() 因为它是 void...

另外,我是否知道到目前为止我是做对了吗?非常感谢您对这个问题的友善帮助

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 和流程线程协调的错误顺序?

my very first post here after long time of searching but yet get an answer regarding this issue, please help me in this issue.

I am using Netbean 6.9.1 to build a Java application which massive call to few different external program, therefore I used process and runtime function to call for external program.

The whole application process is separated into few stages and I wish to inform the user till which stage the application is currently running by updating the GUI textarea, the code is showed as below:

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();
      }

}

As showed in the code above, I wish to set the Textarea and disable some button before executing the command, but when the application ran, all of these line seems unable to work and nothing is changed at the application itself till the command is finish executed, any solution to run the pre-command code execute first before the .exec() start to run?

I appreciate your great help and advice regarding this issue.

Best regards,
Striky

P/S:

hi there, I have make a Thread class for this CmdExec in order to execute cmd in different thread:

     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();
      }

}
}

and in order to call to this class,

CmdExec tryDemo = new CmdExec();
tryDemo = new CmdExec(strSegment, fSegment);
tryDemo.run();
is used to start the thread, but I failed to put SwingUtilities.invokeLater in any part of these process, it simply won't run the tryDemo.run() because it is void...

Also, may I know so far am I doing right?? Very thank you for your kind help regarding this issue

P/S 2: I have just added another runnable code (so threads for process executing, runnable to GUI update) for GUI update command as below:

            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"); }
};

and I used SwingUtilies.invokeLater before the execution of process as below:

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

But all these failed, am I get the wrong sequence for the GUI and process thread coordination?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

债姬 2024-11-04 05:44:03

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

you are executing this work on the EDT (the thread which updates the gui). so, the gui cannot update until all this work finishes. what you want to do is run a separate thread which does all the work and periodically calls SwingUtilities.invokeLater with a status update.

两相知 2024-11-04 05:44:03

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

Try to put sleep before execute method. To verify what is happening.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文