Package org.codehaus.mojo.javacc
Class ForkedJvm
- java.lang.Object
-
- org.codehaus.mojo.javacc.ForkedJvm
-
class ForkedJvm extends java.lang.ObjectRuns themain()method of some tool in a forked JVM.- See Also:
- java - The Java Application Launcher
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Set<java.lang.String>classPathEntriesThe class path entries for the forked JVM, given as strings.private java.util.List<java.lang.String>cmdLineArgsThe command line arguments to pass to themain()method, given as strings.private java.lang.StringexecutableThe executable used to fork the JVM.private java.lang.StringmainClassThe qualified name of the class on which to invoke themain()method.private org.codehaus.plexus.util.cli.StreamConsumersystemErrThe consumer forSystem.errmessages.private org.codehaus.plexus.util.cli.StreamConsumersystemOutThe consumer forSystem.outmessages.private java.io.FileworkingDirectoryThe working directory for the forked JVM.
-
Constructor Summary
Constructors Constructor Description ForkedJvm()Creates a new configuration to fork a JVM.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddArgument(java.io.File argument)Adds the specified file path to the command line for themain()method.voidaddArgument(java.lang.String argument)Adds the specified argument to the command line for themain()method.voidaddArguments(java.lang.String[] arguments)Adds the specified arguments to the command line for themain()method.voidaddClassPathEntry(java.io.File path)Adds the specified path to the class path of the forked JVM.voidaddClassPathEntry(java.lang.Class<?> type)Adds the source JAR of the specified class/interface to the class path of the forked JVM.voidaddClassPathEntry(java.lang.String path)Adds the specified path to the class path of the forked JVM.private org.codehaus.plexus.util.cli.CommandlinecreateCommandLine()Creates the command line for the new JVM based on the current configuration.private java.lang.String[]getArguments()Gets the command line arguments for themain()method.private static java.io.FilegetClassSource(java.lang.Class<?> type)Gets the JAR file or directory that contains the specified class.private static java.io.FilegetClassSource(java.lang.String className)Gets the JAR file or directory that contains the specified class.private static java.lang.StringgetDefaultExecutable()Gets the absolute path to the JVM executable.private static java.io.FilegetResourceSource(java.lang.String resource, java.lang.ClassLoader loader)Gets the JAR file or directory that contains the specified resource.intrun()Forks a JVM using the previously set parameters.voidsetMainClass(java.lang.Class<?> type)Sets the class on which to invoke themain()method.voidsetMainClass(java.lang.String name)Sets the qualified name of the class on which to invoke themain()method.voidsetSystemErr(org.codehaus.plexus.util.cli.StreamConsumer consumer)Sets the stream consumer used to handle messages fromSystem.err.voidsetSystemOut(org.codehaus.plexus.util.cli.StreamConsumer consumer)Sets the stream consumer used to handle messages fromSystem.out.voidsetWorkingDirectory(java.io.File directory)Sets the working directory for the forked JVM.java.lang.StringtoString()Gets a string representation of the command line arguments.
-
-
-
Field Detail
-
systemOut
private org.codehaus.plexus.util.cli.StreamConsumer systemOut
The consumer forSystem.outmessages.
-
systemErr
private org.codehaus.plexus.util.cli.StreamConsumer systemErr
The consumer forSystem.errmessages.
-
executable
private final java.lang.String executable
The executable used to fork the JVM.
-
workingDirectory
private java.io.File workingDirectory
The working directory for the forked JVM.
-
classPathEntries
private final java.util.Set<java.lang.String> classPathEntries
The class path entries for the forked JVM, given as strings.
-
mainClass
private java.lang.String mainClass
The qualified name of the class on which to invoke themain()method.
-
cmdLineArgs
private final java.util.List<java.lang.String> cmdLineArgs
The command line arguments to pass to themain()method, given as strings.
-
-
Method Detail
-
getDefaultExecutable
private static java.lang.String getDefaultExecutable()
Gets the absolute path to the JVM executable.- Returns:
- The absolute path to the JVM executable.
-
setWorkingDirectory
public void setWorkingDirectory(java.io.File directory)
Sets the working directory for the forked JVM.- Parameters:
directory- The working directory for the forked JVM, may benullto inherit the working directory of the current JVM.
-
setSystemOut
public void setSystemOut(org.codehaus.plexus.util.cli.StreamConsumer consumer)
Sets the stream consumer used to handle messages fromSystem.out.- Parameters:
consumer- The stream consumer, may benullto discard the output.
-
setSystemErr
public void setSystemErr(org.codehaus.plexus.util.cli.StreamConsumer consumer)
Sets the stream consumer used to handle messages fromSystem.err.- Parameters:
consumer- The stream consumer, may benullto discard the output.
-
addClassPathEntry
public void addClassPathEntry(java.lang.String path)
Adds the specified path to the class path of the forked JVM.- Parameters:
path- The path to add, may benull.
-
addClassPathEntry
public void addClassPathEntry(java.io.File path)
Adds the specified path to the class path of the forked JVM.- Parameters:
path- The path to add, may benull.
-
addClassPathEntry
public void addClassPathEntry(java.lang.Class<?> type)
Adds the source JAR of the specified class/interface to the class path of the forked JVM.- Parameters:
type- The class/interface to add, may benull.
-
getClassSource
private static java.io.File getClassSource(java.lang.Class<?> type)
Gets the JAR file or directory that contains the specified class.- Parameters:
type- The class/interface to find, may benull.- Returns:
- The absolute path to the class source location or
nullif unknown.
-
getClassSource
private static java.io.File getClassSource(java.lang.String className)
Gets the JAR file or directory that contains the specified class.- Parameters:
className- The qualified name of the class/interface to find, may benull.- Returns:
- The absolute path to the class source location or
nullif unknown.
-
getResourceSource
private static java.io.File getResourceSource(java.lang.String resource, java.lang.ClassLoader loader)Gets the JAR file or directory that contains the specified resource.- Parameters:
resource- The absolute name of the resource to find, may benull.loader- The class loader to use for searching the resource, may benull.- Returns:
- The absolute path to the resource location or
nullif unknown.
-
setMainClass
public void setMainClass(java.lang.String name)
Sets the qualified name of the class on which to invoke themain()method. The source of the specified class will automatically be added to the class path of the forked JVM.- Parameters:
name- The qualified name of the class on which to invoke themain()method.
-
setMainClass
public void setMainClass(java.lang.Class<?> type)
Sets the class on which to invoke themain()method. The source of the specified class will automatically be added to the class path of the forked JVM.- Parameters:
type- The class on which to invoke themain()method, may benull.
-
getArguments
private java.lang.String[] getArguments()
Gets the command line arguments for themain()method.- Returns:
- The command line arguments for the
main()method.
-
addArgument
public void addArgument(java.lang.String argument)
Adds the specified argument to the command line for themain()method.- Parameters:
argument- The argument to add, may benull.
-
addArgument
public void addArgument(java.io.File argument)
Adds the specified file path to the command line for themain()method.- Parameters:
argument- The argument to add, may benull.
-
addArguments
public void addArguments(java.lang.String[] arguments)
Adds the specified arguments to the command line for themain()method.- Parameters:
arguments- The arguments to add, may benull.
-
createCommandLine
private org.codehaus.plexus.util.cli.Commandline createCommandLine()
Creates the command line for the new JVM based on the current configuration.- Returns:
- The command line used to fork the JVM, never
null.
-
run
public int run() throws java.lang.ExceptionForks a JVM using the previously set parameters.- Returns:
- The exit code of the forked JVM.
- Throws:
java.lang.Exception- If the JVM could not be forked.
-
toString
public java.lang.String toString()
Gets a string representation of the command line arguments.- Overrides:
toStringin classjava.lang.Object- Returns:
- A string representation of the command line arguments.
-
-