Running a program from Java code

What is the easiest way to invoke a program using a piece of Java code? (The program I want to run is aiSee, and it can be launched from the command line or from the Windows GUI, and I am in Vista, but the code will also run on Linux systems).

+1
source share
4 answers

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

Here is a small piece of code that may be useful:

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 SO discussing similar issues. Another one . And one more.

+7

Runtime.getRuntime() runtime exec .

:

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

, InputStream .

+1
0

You may also consider passing an argument to your program to make it easier to find the specific program you want to run.

These can be command line arguments, property files, or system properties.

0
source

Source: https://habr.com/ru/post/1756522/


All Articles