Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing a Java application in a separate process

Tags:

java

process

exec

Can a Java application be loaded in a separate process using its name, as opposed to its location, in a platform independent manner?

I know you can execute a program via ...

Process process = Runtime.getRuntime().exec( COMMAND ); 

... the main issue of this method is that such calls are then platform specific.


Ideally, I'd wrap a method into something as simple as...

EXECUTE.application( CLASS_TO_BE_EXECUTED ); 

... and pass in the fully qualified name of an application class as CLASS_TO_BE_EXECUTED.

like image 229
Ande Turner Avatar asked Mar 11 '09 21:03

Ande Turner


People also ask

How do I run a Java program from another application?

In JavaSW, you can run another application by calling the exec method on the Runtime object, which may be obtained via a call to Runtime. getRuntime().

Is the JVM a separate process?

The JVM is always a single process started by java ... . There are containers allowing to run multiple applications in a single JVM, like e.g. Servlets, where each Servlet is mostly isolated from the others (they still share memory, CPU, and some global data).

What is subprocess in Java?

The subprocess is started with its IO passing up to the process that executed it. This is required to see both any stdout s and stderr s it produces. inheritIO is a convenience method and can also be achieved by calling chaining the following code instead (also configures the stdin of the subprocess): builder .


1 Answers

This is a synthesis of some of the other answers that have been provided. The Java system properties provide enough information to come up with the path to the java command and the classpath in what, I think, is a platform independent way.

public final class JavaProcess {      private JavaProcess() {}              public static int exec(Class klass, List<String> args) throws IOException,                                                InterruptedException {         String javaHome = System.getProperty("java.home");         String javaBin = javaHome +                 File.separator + "bin" +                 File.separator + "java";         String classpath = System.getProperty("java.class.path");         String className = klass.getName();          List<String> command = new LinkedList<String>();         command.add(javaBin);         command.add("-cp");         command.add(classpath);         command.add(className);         if (args != null) {             command.addAll(args);         }          ProcessBuilder builder = new ProcessBuilder(command);          Process process = builder.inheritIO().start();         process.waitFor();         return process.exitValue();     }  } 

You would run this method like so:

int status = JavaProcess.exec(MyClass.class, args); 

I thought it made sense to pass in the actual class rather than the String representation of the name since the class has to be in the classpath anyways for this to work.

like image 103
hallidave Avatar answered Oct 08 '22 20:10

hallidave