public final class JavaProcess
extends java.lang.Object
List<String> commands = new ArrayList<String>();
commands.add("myJar.jar");
JavaProcess jp = new JavaProcess(commands);
jp.execute();
int outValueFromRemoteJVM = jp.get();
The method get() waits for the process to end. The result is actually the output value of the remote Java Virtual Machine (JVM), which is usually
0 if the process ended properly. The class is designed to be run only once. Additional calls of the execute() method
will not result in the instantiation of a new independent Java process. As in the SwingWorker class, this class also
send PropertyChangeEvent to eventual listeners that can be added through the addPropertyChangeListener method.
Objects can be sent to the process using the method sendObjectToProcess(). The objects are sent asynchronously and may not be sent is the process
ends before. The incoming stream, ie. the output stream of the remote JVM, is sent to the PropertyChangeListener instances using the "MessageReceived" property.
Finally, the class implements the Serializable interface. Actually, only some fields are serializable. A JavaProcess instance can be serialized either before or after
executing the process, but not during its execution.| Modifier and Type | Class and Description |
|---|---|
static class |
JavaProcess.JVM_OPTION |
IndependentProcess.StateValue| Constructor and Description |
|---|
JavaProcess(java.util.List<java.lang.String> commands)
Sub constructor with the commands only.
|
JavaProcess(java.util.List<java.lang.String> commands,
java.io.File workingDirectory)
Sub constructor with only the commands and the working directory.
|
JavaProcess(java.util.List<java.lang.String> commands,
java.io.File workingDirectory,
java.util.Map<java.lang.String,java.lang.String> environment)
General constructor.
|
JavaProcess(java.util.List<java.lang.String> commands,
java.util.Map<java.lang.String,java.lang.String> environment)
Sub constructor with only the commands and the environment.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addPropertyChangeListener(java.beans.PropertyChangeListener listener)
This method add a listener to the instance of this class if it is not already included in the listeners list.
|
boolean |
cancel(boolean mayInterruptIfRunning) |
protected java.lang.Process |
createIndependentProcess()
This method creates the proper Process instance according to the derived class.
|
void |
execute()
This method starts the independent process.
|
protected void |
firePropertyChange(java.lang.String propertyName,
java.lang.Object oldValue,
java.lang.Object newValue) |
java.lang.Integer |
get() |
java.lang.Integer |
get(long duration,
java.util.concurrent.TimeUnit timeUnit) |
java.util.Map<JavaProcess.JVM_OPTION,java.lang.String> |
getJVMSettings()
This method returns the options of the JVM as set by the user.
|
java.lang.String |
getName()
This method returns the name of the process.
|
IndependentProcess.StateValue |
getState()
This method returns the state of the process which can be either PENDING, STARTED, or DONE.
|
boolean |
isCancelled() |
boolean |
isDone() |
void |
redirectOutputStream(boolean redirectOutputStream)
This method enables the redirection of the outputstream of the independent process into the outputstream of this outer process (this process).
|
void |
removePropertyChangeListener(java.beans.PropertyChangeListener listener)
This method removes all the instances of the listener among the listeners of this class.
|
void |
run() |
void |
sendObjectToProcess(java.io.Serializable obj)
This method makes it possible to send a Serializable object to the independent process, typically a callback process.
|
void |
setClassPath(java.lang.String classPath)
This method sets the classpath.
|
void |
setJVMMemory(int nbMegaJVM)
This method sets the maximum memory of the Java Virtual Machine that runs this process.
|
void |
setName(java.lang.String name)
This method sets the name of this process.
|
void |
setOpenModuleForVersionsLaterThan8Enabled(boolean bool)
Open the internal loader for versions later than 8.
|
void |
setOption(JavaProcess.JVM_OPTION optionName,
java.lang.String option)
This method sets the JVM options
|
public JavaProcess(java.util.List<java.lang.String> commands,
java.io.File workingDirectory,
java.util.Map<java.lang.String,java.lang.String> environment)
commands - the list of commandsworkingDirectory - the working directory for the processenvironment - the environment of the processpublic JavaProcess(java.util.List<java.lang.String> commands,
java.io.File workingDirectory)
commands - the list of commandsworkingDirectory - the working directory for the processpublic JavaProcess(java.util.List<java.lang.String> commands)
commands - the list of commandspublic JavaProcess(java.util.List<java.lang.String> commands,
java.util.Map<java.lang.String,java.lang.String> environment)
commands - the list of commandsenvironment - the environment of the processpublic java.lang.String getName()
IndependentProcessgetName in interface IndependentProcessprotected java.lang.Process createIndependentProcess()
throws java.io.IOException
java.io.IOExceptionpublic void setJVMMemory(int nbMegaJVM)
nbMegaJVM - the number of MegaByte (Integer)public void setClassPath(java.lang.String classPath)
classPath - the complete classpathpublic void setOpenModuleForVersionsLaterThan8Enabled(boolean bool)
bool - it is set to false by defaultpublic void setOption(JavaProcess.JVM_OPTION optionName, java.lang.String option)
optionName - a JVM_OPTION enum instanceoption - a String that defines the optionpublic java.util.Map<JavaProcess.JVM_OPTION,java.lang.String> getJVMSettings()
public boolean cancel(boolean mayInterruptIfRunning)
cancel in interface java.util.concurrent.Future<java.lang.Integer>public java.lang.Integer get()
throws java.lang.InterruptedException,
java.util.concurrent.ExecutionException
get in interface java.util.concurrent.Future<java.lang.Integer>java.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionpublic java.lang.Integer get(long duration,
java.util.concurrent.TimeUnit timeUnit)
throws java.lang.InterruptedException,
java.util.concurrent.ExecutionException,
java.util.concurrent.TimeoutException
get in interface java.util.concurrent.Future<java.lang.Integer>java.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionjava.util.concurrent.TimeoutExceptionpublic boolean isCancelled()
isCancelled in interface java.util.concurrent.Future<java.lang.Integer>public IndependentProcess.StateValue getState()
public boolean isDone()
isDone in interface java.util.concurrent.Future<java.lang.Integer>public void run()
run in interface java.lang.Runnableprotected void firePropertyChange(java.lang.String propertyName,
java.lang.Object oldValue,
java.lang.Object newValue)
public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
IndependentProcessaddPropertyChangeListener in interface IndependentProcesslistener - a PropertyChangeListener instancepublic void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
IndependentProcessremovePropertyChangeListener in interface IndependentProcesslistener - a PropertyChangeListener instancepublic void execute()
IndependentProcessexecute in interface IndependentProcesspublic void setName(java.lang.String name)
IndependentProcesssetName in interface IndependentProcessname - a Stringpublic void sendObjectToProcess(java.io.Serializable obj)
throws java.io.IOException
IndependentProcesssendObjectToProcess in interface IndependentProcessobj - a Serializable objectjava.io.IOExceptionpublic void redirectOutputStream(boolean redirectOutputStream)
redirectOutputStream - true to enable or false to disable