从 Java 代码中运行程序

发布于 2024-07-19 14:27:33 字数 103 浏览 9 评论 0原文

使用一段 Java 代码调用程序的最简单方法是什么? (我想要运行的程序是aiSee,它可以从命令行或Windows GUI运行;我使用的是Vista,但代码也可以在Linux系统上运行)。

What is the simplest way to call a program from with a piece of Java code? (The program I want to run is aiSee and it can be run from command line or from Windows GUI; and I am on Vista but the code will also be run on Linux systems).

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

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

发布评论

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

评论(4

不知在何时 2024-07-26 14:27:33

您可以使用 Runtime.getRuntime() 获取运行时实例,并调用运行时的 exec 方法,并将执行程序的命令作为参数。

例如:

Runtime runTime = Runtime.getRuntime ();       
Process proc = rt.exec("iSee.exe");

您还可以通过从进程中获取InputStream来捕获程序的输出。

You can get a runtime instance using Runtime.getRuntime() and call the runtime's exec method, with the command to execute the program as an argument.

For example:

Runtime runTime = Runtime.getRuntime ();       
Process proc = rt.exec("iSee.exe");

You can also capture the output of the program by using getting the InputStream from the process.

慢慢从新开始 2024-07-26 14:27:33

您将遇到的困难是如何让应用程序知道路径。 您可能想要使用 xml 或配置文件,但如果您使用此链接,它应该解释如何运行文件:
http://www.javacoffeebreak.com/faq/faq0030.html

The difficulty you will run into is how to get the application to know the path. You may want to use an xml or config file, but if you use this link, it should explain how to run a file:
http://www.javacoffeebreak.com/faq/faq0030.html

我也只是我 2024-07-26 14:27:33

您可能还需要考虑向您的程序传递某种参数,以方便查找您想要运行的特定程序。

这可以是命令行参数、属性文件或系统属性。

You may also want to consider passing in some kind of argument to your program to facilitate finding the specific program you want to run.

This could be with command line arguments, properties files or system properties.

你的笑 2024-07-26 14:27:33

查看 Process运行时类。 请记住,您想要完成的任务可能不是独立于平台的。

下面是一小段可能有用的代码:

public class YourClass
{
    public static void main(String args[])
       throws Exception
    {
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec("name_of_your_application.exe");
        int exitVal = proc.exitValue();
        System.out.println("Process exitValue: " + exitVal);
    }
}

SO 中的一个 问题讨论类似问题。 另一个还有另一个。

Take a look at Process and Runtime classes. Keep in mind that what you are trying to accomplish is probably not platform independent.

Here is a little piece of code that might be helpful:

public class YourClass
{
    public static void main(String args[])
       throws Exception
    {
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec("name_of_your_application.exe");
        int exitVal = proc.exitValue();
        System.out.println("Process exitValue: " + exitVal);
    }
}

One question in S.O. discussing similiar issues. Another one. And another one.

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