从Java代码中运行程序 [英] Running a program from within Java code

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

问题描述

用一段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).

推荐答案

看看进程运行时类。请记住,您要完成的任务可能与平台无关。

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);
    }
}

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

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

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

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