gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:20:50 24 4
gpt4 key购买 nike

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

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

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

public voidexecuteCommand(字符串cmd,文件路径) {

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

}

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

感谢您对这个问题的大力帮助和建议。

最诚挚的问候,引人注目

附注:

您好,我为此 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 和进程线程协调顺序?

最佳答案

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

关于java - 无法在 Process.Runtime.exec 语句行之前执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5504925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com