Class ForkedJvm

java.lang.Object
org.codehaus.mojo.javacc.ForkedJvm

class ForkedJvm extends Object
Runs the main() method of some tool in a forked JVM.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private Set
    The class path entries for the forked JVM, given as strings.
    private List
    The command line arguments to pass to the main() method, given as strings.
    private String
    The executable used to fork the JVM.
    private String
    The qualified name of the class on which to invoke the main() method.
    private org.codehaus.plexus.util.cli.StreamConsumer
    The consumer for System.err messages.
    private org.codehaus.plexus.util.cli.StreamConsumer
    The consumer for System.out messages.
    private File
    The working directory for the forked JVM.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new configuration to fork a JVM.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addArgument(File argument)
    Adds the specified file path to the command line for the main() method.
    void
    addArgument(String argument)
    Adds the specified argument to the command line for the main() method.
    void
    addArguments(String[] arguments)
    Adds the specified arguments to the command line for the main() method.
    void
    Adds the specified path to the class path of the forked JVM.
    void
    Adds the source JAR of the specified class/interface to the class path of the forked JVM.
    void
    Adds the specified path to the class path of the forked JVM.
    private org.codehaus.plexus.util.cli.Commandline
    Creates the command line for the new JVM based on the current configuration.
    private String[]
    Gets the command line arguments for the main() method.
    private String
    Gets the class path for the forked JVM.
    private static File
    Gets the JAR file or directory that contains the specified class.
    private static File
    Gets the JAR file or directory that contains the specified class.
    private static String
    Gets the absolute path to the JVM executable.
    private static File
    Gets the JAR file or directory that contains the specified resource.
    int
    run()
    Forks a JVM using the previously set parameters.
    void
    Sets the class on which to invoke the main() method.
    void
    Sets the qualified name of the class on which to invoke the main() method.
    void
    setSystemErr(org.codehaus.plexus.util.cli.StreamConsumer consumer)
    Sets the stream consumer used to handle messages from System.err.
    void
    setSystemOut(org.codehaus.plexus.util.cli.StreamConsumer consumer)
    Sets the stream consumer used to handle messages from System.out.
    void
    Sets the working directory for the forked JVM.
    Gets a string representation of the command line arguments.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • systemOut

      private org.codehaus.plexus.util.cli.StreamConsumer systemOut
      The consumer for System.out messages.
    • systemErr

      private org.codehaus.plexus.util.cli.StreamConsumer systemErr
      The consumer for System.err messages.
    • executable

      private String executable
      The executable used to fork the JVM.
    • workingDirectory

      private File workingDirectory
      The working directory for the forked JVM.
    • classPathEntries

      private Set classPathEntries
      The class path entries for the forked JVM, given as strings.
    • mainClass

      private String mainClass
      The qualified name of the class on which to invoke the main() method.
    • cmdLineArgs

      private List cmdLineArgs
      The command line arguments to pass to the main() method, given as strings.
  • Constructor Details

    • ForkedJvm

      public ForkedJvm()
      Creates a new configuration to fork a JVM.
  • Method Details

    • getDefaultExecutable

      private static String getDefaultExecutable()
      Gets the absolute path to the JVM executable.
      Returns:
      The absolute path to the JVM executable.
    • setWorkingDirectory

      public void setWorkingDirectory(File directory)
      Sets the working directory for the forked JVM.
      Parameters:
      directory - The working directory for the forked JVM, may be null to 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 from System.out.
      Parameters:
      consumer - The stream consumer, may be null to discard the output.
    • setSystemErr

      public void setSystemErr(org.codehaus.plexus.util.cli.StreamConsumer consumer)
      Sets the stream consumer used to handle messages from System.err.
      Parameters:
      consumer - The stream consumer, may be null to discard the output.
    • getClassPath

      private String getClassPath()
      Gets the class path for the forked JVM.
      Returns:
      The class path for the forked JVM.
    • addClassPathEntry

      public void addClassPathEntry(String path)
      Adds the specified path to the class path of the forked JVM.
      Parameters:
      path - The path to add, may be null.
    • addClassPathEntry

      public void addClassPathEntry(File path)
      Adds the specified path to the class path of the forked JVM.
      Parameters:
      path - The path to add, may be null.
    • addClassPathEntry

      public void addClassPathEntry(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 be null.
    • getClassSource

      private static File getClassSource(Class type)
      Gets the JAR file or directory that contains the specified class.
      Parameters:
      type - The class/interface to find, may be null.
      Returns:
      The absolute path to the class source location or null if unknown.
    • getClassSource

      private static File getClassSource(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 be null.
      Returns:
      The absolute path to the class source location or null if unknown.
    • getResourceSource

      private static File getResourceSource(String resource, ClassLoader loader)
      Gets the JAR file or directory that contains the specified resource.
      Parameters:
      resource - The absolute name of the resource to find, may be null.
      loader - The class loader to use for searching the resource, may be null.
      Returns:
      The absolute path to the resource location or null if unknown.
    • setMainClass

      public void setMainClass(String name)
      Sets the qualified name of the class on which to invoke the main() 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 the main() method.
    • setMainClass

      public void setMainClass(Class type)
      Sets the class on which to invoke the main() 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 the main() method, may be null.
    • getArguments

      private String[] getArguments()
      Gets the command line arguments for the main() method.
      Returns:
      The command line arguments for the main() method.
    • addArgument

      public void addArgument(String argument)
      Adds the specified argument to the command line for the main() method.
      Parameters:
      argument - The argument to add, may be null.
    • addArgument

      public void addArgument(File argument)
      Adds the specified file path to the command line for the main() method.
      Parameters:
      argument - The argument to add, may be null.
    • addArguments

      public void addArguments(String[] arguments)
      Adds the specified arguments to the command line for the main() method.
      Parameters:
      arguments - The arguments to add, may be null.
    • 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 Exception
      Forks a JVM using the previously set parameters.
      Returns:
      The exit code of the forked JVM.
      Throws:
      Exception - If the JVM could not be forked.
    • toString

      public String toString()
      Gets a string representation of the command line arguments.
      Overrides:
      toString in class Object
      Returns:
      A string representation of the command line arguments.