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

JavaExecExternal Process

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

for POSIX-Compatible system:

 Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", "your command"}).waitFor();

Solution 2 - Java

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'

Solution 3 - Java

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.

Solution 4 - Java

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

Solution 5 - Java

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.

Solution 6 - Java

I tried everything mentioned here but without success. Main parent Java process can't quit until the quit of subthread even with cmd /c start and redirecting streams tu nul.

Only one reliable solution for me is this:

try {
    Runtime.getRuntime().exec("psexec -i cmd /c start cmd.cmd");
}
catch (Exception e) {
    // handle it
}

I know that this is not clear, but this small utility from SysInternals is very helpful and proven. Here is the link.

Solution 7 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLawrence DolView Question on Stackoverflow
Solution 1 - JavaLudwig WeinzierlView Answer on Stackoverflow
Solution 2 - JavaManvendra GuptaView Answer on Stackoverflow
Solution 3 - JavamonceauxView Answer on Stackoverflow
Solution 4 - JavaCharlie MartinView Answer on Stackoverflow
Solution 5 - JavaMatthew FlaschenView Answer on Stackoverflow
Solution 6 - JavaMrojView Answer on Stackoverflow
Solution 7 - JavaDarren GreavesView Answer on Stackoverflow