[Q#931536][A#931663] How do I launch a completely independent process from a Java program?
https://stackoverflow.com/q/931536
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. 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.
Answer
https://stackoverflow.com/a/931663
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. And tested with the following on Linux: where test.sh looks like: as well as this on Linux: And tested this on Windows: 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 : 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.
APIzation
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
Runtime.getRuntime().exec(args[0]);
}
}
java -jar JustForTesting.jar /home/monceaux/Desktop/__TMP/test.sh
#!/bin/bash
ping -i 20 localhost
java -jar JustForTesting.jar gedit
java -jar JustForTesting.jar notepad.exe
package com.stackoverflow.api;
public class Human931663 {
/**
* @param command the command line arguments
*/
public static void executeJavaProcess(String str1) throws Exception {
Runtime.getRuntime().exec(str1);
}
}
package com.stackoverflow.api;
/**
* How do I launch a completely independent process from a Java program?
*
* @author APIzator
* @see <a href="https://stackoverflow.com/a/931663">https://stackoverflow.com/a/931663</a>
*/
public class APIzator931663 {
/**
* @param args the command line arguments
*/
public static void launchProcess(String str1) throws Exception {
Runtime.getRuntime().exec(str1);
}
}