Alex Rivera | Logout

How do I launch a completely independent process from a Java program?

Asked 2009-05-31T07:15:01.727
61

I am working on a program written in Java which, for some actions, launches external programs using user-configured command lines. Currently it uses Runtime.exec() and does not retain the Process reference (the launched programs are either a text editor or archive utility, so no need for the system in/out/err streams).

There is a minor problem with this though, in that when the Java program exits, it doesn't really quit until all the launched programs are exited.

I would greatly prefer it if the launched programs were completely independent of the JVM which launched them.

The target operating system is multiple, with Windows, Linux and Mac being the minimum, but any GUI system with a JVM is really what is desired (hence the user configurability of the actual command lines).

Does anyone know how to make the launched program execute completely independently of the JVM?


Edit in response to a comment

The launch code is as follows. The code may launch an editor positioned at a specific line and column, or it may launch an archive viewer. Quoted values in the configured command line are treated as ECMA-262 encoded, and are decoded and the quotes stripped to form the desired exec parameter.

The launch occurs on the EDT.

static Throwable launch(String cmd, File fil, int lin, int col) throws Throwable {
    String frs[][]={
        { "$FILE$"  ,fil.getAbsolutePath().replace('\\','/') },
        { "$LINE$"  ,(lin>0 ? Integer.toString(lin) : "") },
        { "$COLUMN$",(col>0 ? Integer.toString(col) : "") },
        };
    String[] arr; // array of parsed tokens (exec(cmd) does not handle quoted values)

    cmd=TextUtil.replace(cmd,frs,true,"$$","$");
    arr=(String[])ArrayUtil.removeNulls(TextUtil.stringComponents(cmd,' ',-1,true,true,true));
    for(int xa=0; xa<arr.length; xa++) {
        if(TextUtil.isQuoted(arr[xa],true)) {
 
Edit
Report

2 Answers

26

I have some observations that may help other people facing similar issue.

When you use Runtime.getRuntime().exec() and then you ignore the java.lang.Process handle you get back (like in the code from original poster), there is a chance that the launched process may hang.

I have faced this issue in Windows environment and traced the problem to the stdout and stderr streams. If the launched application is writing to these streams, and the buffer for these stream fills up then the launched application may appear to hang when it tries to write to the streams. The solutions are:

  1. Capture the Process handle and empty out the streams continually - but if you want to terminate the java application right after launching the process then this is not a feasible solution
  2. Execute the process call as cmd /c <<process>> (this is only for Windows environment).
  3. Suffix the process command and redirect the stdout and stderr streams to nul using 'command > nul 2>&1'
answered 2012-06-13T12:36:31.267
-2

One way I can think of is to use Runtime.addShutdownHook to register a thread that kills off all the processes (you'd need to retain the process objects somewhere of course).

The shutdown hook is only called when the JVM exits so it should work fine.

A little bit of a hack but effective.

answered 2009-05-31T07:59:45.707

Your Answer