Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 762
Lawrence Dol Avatar asked May 31 '09 07:05

Lawrence Dol


People also ask

How can we invoke an external process in Java?

Using the ProcessBuilder class Instantiate the ProcessBuilder class by passing the command to execute a process and arguments for it as parameters to its constructor. Execute the process by invoking the start() method on the above created object.

How do you execute a process in Java?

Process process = Runtime. getRuntime(). exec("processname"); Both of these will code snippets will spawn a new process, which usually executes asynchronously and can be interacted with through the resulting Process object.

How do you terminate a process in Java?

If you start the process from with in your Java application (ex. by calling Runtime. exec() or ProcessBuilder. start() ) then you have a valid Process reference to it, and you can invoke the destroy() method in Process class to kill that particular process.


1 Answers

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(); 
like image 62
Ludwig Weinzierl Avatar answered Oct 04 '22 19:10

Ludwig Weinzierl