views:

1949

answers:

5

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)) {
            arr[xa]=TextDecode.ecma262(TextUtil.stripQuotes(arr[xa]));
            }
        }
    log.println("Launching: "+cmd);
    Runtime.getRuntime().exec(arr);
    return null;
    }

This appears to be happening only when the program is launched from my IDE. I am closing this question since the problem exists only in my development environment; it is not a problem in production. From the test program in one of the answers, and further testing I have conducted I am satisfied that it is not a problem that will be seen by any user of the program on any platform.

A: 

You want to launch the program in the background, and separate it from the parent. I'd consider nohup(1).

Charlie Martin
Well, for Linux, sure... But the problem isn't that the launched process is being terminated, it's that the JVM won't end until the launched program(s) is(are) also ended.
Software Monkey
DId you actually *try* it? A nohup'ed background process should appear to the system() call as if it immediately terminated.
Charlie Martin
+6  A: 

There is a parent child relation between your processes and you have to break that. For Windows you can try:

Runtime.getRuntime().exec("cmd /c start editor.exe");

For Linux the process seem to run detached anyway, no nohup necessary. I tried it with gvim, midori and acroread.

import java.io.IOException;
public class Exec {
    public static void main(String[] args) {
     try {
      Runtime.getRuntime().exec("/usr/bin/acroread");
     } catch (IOException e) {
      e.printStackTrace();
     }
     System.out.println("Finished");
    }
}

I think it is not possible to to it with Runtime.exec in a platform independent way.

Ludwig Weinzierl
I was hoping there would be something I could do in the process creation... this requires the user to configure the "correct" command line. But good to know.
Software Monkey
You can have the user configure the correct command line and and sneakily add the operating system specific magic when necessary.
Ludwig Weinzierl
the "cmd /c start" idea is awesome and helped a ton. thanks.
Epaga
A: 

I suspect this would require a actual process fork. Basically, the C equivalent of what you want is:

pid_t id = fork();
if(id == 0)
  system(command_line);

The problem is you can't do a fork() in pure Java. What I would do is:

Thread t = new Thread(new Runnable()
{
    public void run()
    {
   try
   {
       Runtime.getRuntime().exec(command);
   }
   catch(IOException e)
   {        
       // Handle error.
       e.printStackTrace();
   }
    }
});
t.start();

That way the JVM still won't exit, but no GUI and only a limited memory footprint will remain.

Matthew Flaschen
A: 

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.

Darren Greaves
Except I don't want to kill the processes. What I want is for them to not prevent the JVM from exiting. I think my users would be quite surprised at exiting my app and seeing several other programs disappear along with it.
Software Monkey
Oh yeah, hmm, good point. :-)
Darren Greaves
+2  A: 

It may help if you post a test section of minimal code needed to reproduce the problem. I tested the following code on Windows and a Linux system.

public class Main
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception
    {
        Runtime.getRuntime().exec(args[0]);
    }
}

And tested with the following on Linux:

java -jar JustForTesting.jar /home/monceaux/Desktop/__TMP/test.sh

where test.sh looks like:

#!/bin/bash
ping -i 20 localhost

as well as this on Linux:

java -jar JustForTesting.jar gedit

And tested this on Windows:

java -jar JustForTesting.jar notepad.exe

All of these launched their intended programs, but the Java application had no problems exiting. I have the following versions of Sun's JVM as reported by java -version :

  • Windows: 1.6.0_13-b03
  • Linux: 1.6.0_10-b33

I have not had a chance to test on my Mac yet. Perhaps there is some interaction occuring with other code in your project that may not be clear. You may want to try this test app and see what the results are.

monceaux
This problem might be limited to Windows XP, or it might only be a result of the way the JVM is launched from my IDE during testing. On Windows Vista, it is not happening for the distributed build. After testing on XP, this answer will be accepted if it shows a problem also only when the JVM is launched from the IDE.
Software Monkey
This appears only to be happening when launched from my IDE, so I am accepting this answer as indicating that my problem exists only in my development environment; it is not a problem in production.
Software Monkey
Monceaux: Thanks, BTW, this simple test was what made me look closer at the precise conditions under which the program didn't exit - from which I realized it was my *IDE* launcher and not Java which was not exiting.
Software Monkey