从我的Java工具启动execution.exe

从我的Java工具启动execution.exe,java,multithreading,user-interface,netbeans,exe,Java,Multithreading,User Interface,Netbeans,Exe,我的工具带有Java的用户界面。如果我点击一个按钮,我想启动一个应用程序。exe在我的工具中,我有一个控制台,我想把.exe的输出(控制台)放在那里。为此,我已这样做: Runtime run = Runtime.getRuntime(); StyledDocument doc = txtResult.getStyledDocument(); SimpleAttributeSet keyWord = new SimpleAttributeSet(); StyleConst

我的工具带有Java的用户界面。如果我点击一个按钮,我想启动一个应用程序。exe在我的工具中,我有一个控制台,我想把.exe的输出(控制台)放在那里。为此,我已这样做:

Runtime run = Runtime.getRuntime();
    StyledDocument doc = txtResult.getStyledDocument();
    SimpleAttributeSet keyWord = new SimpleAttributeSet();
    StyleConstants.setForeground(keyWord, Color.RED);

try {

    Process pp=run.exec(PATHEXE);

    BufferedReader in =new BufferedReader(new InputStreamReader(pp.getInputStream()));
    BufferedReader inErr =new BufferedReader(new InputStreamReader(pp.getErrorStream()));
    String line = null;
    String lineErr = null;
    while (((line = in.readLine()) != null) ||(lineErr = inErr.readLine()) != null) {
        if(line != null)
             doc.insertString(doc.getLength(), line+"\n", null );
        if(lineErr != null)
              doc.insertString(doc.getLength(), lineErr+"\n", keyWord );
    }

    int exitVal = pp.waitFor();
    System.out.println("Process exitValue: " + exitVal);
    btRun.setEnabled(true);
}   catch (Exception e) {
    e.printStackTrace();
    System.out.println(e.getMessage());
}
一切正常,但我仍有一些问题:

  • 我想动议执行死刑。另一个线程上的exe 在.exe执行期间释放用户界面
  • 在我的工具中读取output.exe仅在。。。然后运行更新,在运行时获取输出

  • 使用一个。它提供了从EDT中删除长时间运行的任务的功能,同时更新EDT上的GUI。

    运行线程中的任务很容易:

    // put up a progress dialog or something here
    
    Runnable backgroundJob = new Runnable() {
        public void run() {
            final String output = executeJob();  // call your code you provided
            SwingUtilities.invokeLater( new Runnable() {
                public void run() {
                    // safely do your updates to the UI here on the Swing Thread
                    // hide progress dialog here
                    updateUI( output );
                }
            });
        }
    };
    Thread backgroundThread = new Thread( backgroundJob );
    backgroundThread.start();
    

    如果您不想在.exe执行时阻止UI,只需在子线程中运行.exec即可。此外,您可以向子线程注册回调函数,它可以在执行结束时回调该子线程