edu.rice.cs.util.newjvm
Class ExecJVM

java.lang.Object
  extended by edu.rice.cs.util.newjvm.ExecJVM

public final class ExecJVM
extends Object

A utility class to allow executing another JVM.

Version:
$Id: ExecJVM.java 4447 2008-04-18 16:06:34Z rcartwright $

Method Summary
static String getExecutable()
          Find the java executable.
static void printOutput(Process theProc, String msg, String sourceName)
          Prints the stdout and stderr of the given process, line by line.
static Process runJVM(String mainClass, String[] classParams, String[] classPath, String[] jvmParams, File workDir)
          Runs a new JVM.
static Process runJVM(String mainClass, String[] classParams, String classPath, String[] jvmParams, File workDir)
          Runs a new JVM.
static Process runJVMPropagateClassPath(String mainClass, String[] classParams, File workDir)
          Runs a new JVM, propagating the present classpath.
static Process runJVMPropagateClassPath(String mainClass, String[] classParams, String[] jvmParams, File workDir)
          Runs a new JVM, propagating the present classpath.
static void ventBuffers(Process theProc, LinkedList<String> outLines, LinkedList<String> errLines)
          Empties BufferedReaders by copying lines into LinkedLists.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

runJVM

public static Process runJVM(String mainClass,
                             String[] classParams,
                             String[] classPath,
                             String[] jvmParams,
                             File workDir)
                      throws IOException
Runs a new JVM.

Parameters:
mainClass - Class to run
classParams - Parameters to pass to the main class
classPath - Array of items to put in classpath of new JVM
jvmParams - Array of additional command-line parameters to pass to JVM
Returns:
Process object corresponding to the executed JVM
Throws:
IOException

runJVM

public static Process runJVM(String mainClass,
                             String[] classParams,
                             String classPath,
                             String[] jvmParams,
                             File workDir)
                      throws IOException
Runs a new JVM.

Parameters:
mainClass - Class to run
classParams - Parameters to pass to the main class
classPath - Pre-formatted classpath parameter
jvmParams - Array of additional command-line parameters to pass to JVM
Returns:
Process object corresponding to the executed JVM
Throws:
IOException

runJVMPropagateClassPath

public static Process runJVMPropagateClassPath(String mainClass,
                                               String[] classParams,
                                               String[] jvmParams,
                                               File workDir)
                                        throws IOException
Runs a new JVM, propagating the present classpath. It changes the entries in the class path to absolute form.

Parameters:
mainClass - Class to run
classParams - Parameters to pass to the main class
jvmParams - Array of additional command-line parameters to pass to JVM
Returns:
Process object corresponding to the executed JVM
Throws:
IOException

runJVMPropagateClassPath

public static Process runJVMPropagateClassPath(String mainClass,
                                               String[] classParams,
                                               File workDir)
                                        throws IOException
Runs a new JVM, propagating the present classpath.

Parameters:
mainClass - Class to run
classParams - Parameters to pass to the main class
Returns:
Process object corresponding to the new JVM process
Throws:
IOException

ventBuffers

public static void ventBuffers(Process theProc,
                               LinkedList<String> outLines,
                               LinkedList<String> errLines)
                        throws IOException
Empties BufferedReaders by copying lines into LinkedLists. This is intended for use with the output streams from an ExecJVM process. Source and destination objects are specified for stdout and for stderr.

Parameters:
theProc - a Process object whose output will be handled
outLines - the LinkedList of Strings to be filled with the lines read from outBuf
errLines - the LinkedList of Strings to be filled with the lines read from errBuf
Throws:
IOException

printOutput

public static void printOutput(Process theProc,
                               String msg,
                               String sourceName)
                        throws IOException
Prints the stdout and stderr of the given process, line by line. Adds a message and tag to identify the source of the output. Note that this code will print all available stdout before all stderr, since it is impossible to determine in which order lines were added to the respective buffers.

Parameters:
theProc - a Process object whose output will be handled
msg - an initial message to print before output
sourceName - a short string to identify the process
Throws:
IOException - if there is a problem with the streams

getExecutable

public static String getExecutable()
Find the java executable. This logic comes from Ant.