从 Java 执行另一个应用程序 [英] Executing another application from Java

查看:48
本文介绍了从 Java 执行另一个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to execute a batch file which executes another Java application. I don't care whether it executes successfully or not and I don't have to capture any errors.

是否可以使用 ProcessBuilder?如果我不捕获错误会有什么后果?

Is it possible to do this with ProcessBuilder? What are the consequences if I do not capture errors?

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

However, my requirement is just to execute another Java application.

推荐答案

Runtime.getRuntime().exec() 方法很麻烦,你很快就会发现.

The Runtime.getRuntime().exec() approach is quite troublesome, as you'll find out shortly.

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

Take a look at the Apache Commons Exec project. It abstracts you way of a lot of the common problems associated with using the Runtime.getRuntime().exec() and ProcessBuilder API.

就这么简单:

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

这篇关于从 Java 执行另一个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆