gpt4 book ai didi

java - 从 Java 执行另一个应用程序

转载 作者:行者123 更新时间:2023-11-30 17:18:28 27 4
gpt4 key购买 nike

我需要执行一个批处理文件来执行另一个Java应用程序。我不在乎它是否执行成功,也不必捕获任何错误。

是否可以使用ProcessBuilder来做到这一点?如果我不捕获错误会产生什么后果?

但是,我的要求只是执行另一个 Java 应用程序。

最佳答案

Runtime.getRuntime().exec() 方法非常麻烦,您很快就会发现。

看看Apache Commons Exec项目。它为您抽象出了与使用 Runtime.getRuntime().exec() 和 ProcessBuilder API 相关的许多常见问题。

很简单:

String line = "myCommand.exe";
CommandLine commandLine = CommandLine.parse(line);
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
int exitValue = executor.execute(commandLine);

关于java - 从 Java 执行另一个应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29169401/

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