gpt4 book ai didi

java - 从我的 Java 工具启动执行 .exe

转载 作者:行者123 更新时间:2023-12-01 15:41:41 24 4
gpt4 key购买 nike

我的工具带有 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());
}

一切正常,但我仍然有一些问题:

  1. 我想转移执行。 exe 在另一个线程上在.exe执行期间释放用户界面。
  2. 在我的工具中读取输出 .exe 仅在...的末尾,然后运行更新我在运行时获得输出?

最佳答案

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

关于java - 从我的 Java 工具启动执行 .exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7939573/

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