gpt4 book ai didi

java - 从 java 运行 .exe 并继续

转载 作者:行者123 更新时间:2023-12-01 12:19:37 25 4
gpt4 key购买 nike

如果 .exe 程序尚未运行,我想以编程方式运行它。目前,我使用进程生成器从 java 启动 .bat。批处理文件在检查 .exe 是否正在运行后运行它。问题是,在用户关闭 .exe 程序之前,java 不会继续运行。我该如何解决这个问题?

Java 代码:

try {
ProcessBuilder builder = new ProcessBuilder("open.bat");
final Process process = builder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while ((br.readLine()) != null) {
}
System.out.println("command line terminated");
} catch (Exception err) {
err.printStackTrace();
System.out.println("error ");
}

批处理代码:(open.bat)

tasklist | find "illustrator.exe"
if errorlevel 1 start "" "C:\Program Files (x86)\Adobe\Adobe Illustrator CS6\Support Files\Contents\Windows\illustrator.exe"

最佳答案

已更新

使用...

start "" "C:\Program Files (x86)\Adobe\Adobe Illustrator CS4\Support Files\Contents\Windows\illustrator.exe"

作为open.bat批处理文件并使用...

try {
ProcessBuilder builder = new ProcessBuilder("open.bat");
builder.redirectError();
builder.inheritIO();
final Process process = builder.start();
final InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String text = null;
while ((text = br.readLine()) != null) {
System.out.println(text);
}

System.out.println("command line terminated");
System.out.println("Exited with " + process.waitFor());
} catch (Exception err) {
err.printStackTrace();
System.out.println("error ");
}

我可以让批处理文件运行并终止,并且仍然可以加载和运行 Illustrator。

主要的添加是对 ProcessBuilder#inheritIO 的调用,它做了一些我不知道的魔法,但似乎允许批处理文件终止......

来自JavaDocs

This gives behavior equivalent to most operating system command interpreters, or the standard C library function system().

关于java - 从 java 运行 .exe 并继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26792004/

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