通过 java 应用程序从内部调用命令行工具

发布于 2024-10-27 12:05:54 字数 142 浏览 1 评论 0原文

我有一个命令行工具,我想从 java 应用程序启动它。然后,我的应用程序应该等待命令行工具返回/完成。 我将在 Windows、Mac 和 Linux 上部署我的应用程序,并且我的应用程序应该能够在每个平台上调用命令行工具。 如何从我的 java 应用程序中正确调用它?

I have a command line tool that I want to launch from a java application. My application should then wait for the command line tool to return/finish.
I will deploy my application on windows, mac and linux and my app should be able to invoke the command line tool on every platform.
How can I properly invoke it from my java application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栖迟 2024-11-03 12:05:54

http://download.oracle.com/javase /1.5.0/docs/api/java/lang/ProcessBuilder.html

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Process p = pb.start();
p.waitFor();

http://download.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Process p = pb.start();
p.waitFor();
万水千山粽是情ミ 2024-11-03 12:05:54

使用 java.lang.Process 来实现:

    final Process process = Runtime.getRuntime().exec("yourprogram", null, outputDir);
    final int exitCode = process.waitFor();
    if (exitCode != 0) {
        throw new RuntimeException("program didnt exit with 0, but with " + exitCode);
    }

Use java.lang.Process for that:

    final Process process = Runtime.getRuntime().exec("yourprogram", null, outputDir);
    final int exitCode = process.waitFor();
    if (exitCode != 0) {
        throw new RuntimeException("program didnt exit with 0, but with " + exitCode);
    }
寻梦旅人 2024-11-03 12:05:54

您可以使用Runtime类来启动命令行程序。通过确保正在运行的命令行程序始终位于 PATH 中,您应该能够在 Win/Mac/Linux 中使用。

            Runtime rt = Runtime.getRuntime();
            Process proc;               
            proc = rt.exec(cmdName);

            // Wait for the command to complete.
            exitVal = proc.waitFor();

You can use the Runtime class to start command line program. You should be able to use in Win/Mac/Linux by making sure that the command line program you are running is always in the PATH.

            Runtime rt = Runtime.getRuntime();
            Process proc;               
            proc = rt.exec(cmdName);

            // Wait for the command to complete.
            exitVal = proc.waitFor();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文