Class Launcher


  • public class Launcher
    extends Object
    Launches a new JVM process with the specified memory (-memory <amount>), a Java agent (-javaagent <jar-file>) and main class (-main <classname>).

    All other command-line arguments are passed on to the new process.

    In addition to parameters from the commandline, additional parameters can be defined in the ADAMS_OPTS environment variable. Note: No checks are performed if the same parameter is defined in this env variable and on the commandline.

    The ADAMS_LIBRARY_PATH allows you to supply additional libraries that you would normally supply to the JVM using '-Djava.library.path=...'.

    Use -help to output all available parameters.

    When run from commandline, the method addShutdownHook() gets called automatically before execute(), which adds a hook thread to the runtime, killing the launched process, e.g., when using ctrl+c.
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    See Also:
    ENV_ADAMS_OPTS
    • Field Detail

      • CODE_RESTART

        public static final int CODE_RESTART
        the return code for restarting the main class.
        See Also:
        Constant Field Values
      • CODE_RESTART_MORE_HEAP

        public static final int CODE_RESTART_MORE_HEAP
        the return code for restarting the main class with 50% more heap.
        See Also:
        Constant Field Values
      • ENV_ADAMS_OPTS

        public static final String ENV_ADAMS_OPTS
        the environment variable with additional options.
        See Also:
        Constant Field Values
      • ENV_ADAMS_LIBRARY_PATH

        public static final String ENV_ADAMS_LIBRARY_PATH
        the environment variable for the library path.
        See Also:
        Constant Field Values
      • m_Memory

        protected String m_Memory
        the amount of memory to use for the process.
      • m_MainClass

        protected String m_MainClass
        the main class to launch.
      • m_JavaAgentJar

        protected String m_JavaAgentJar
        the java agent jar to use.
      • m_JVMOptions

        protected List<String> m_JVMOptions
        optional JVM options.
      • m_ClassPathAugmentations

        protected List<String> m_ClassPathAugmentations
        optional classpath augmentations.
      • m_EnvironmentModifiers

        protected List<EnvironmentModifier> m_EnvironmentModifiers
        optional environment modifiers.
      • m_PriorityJars

        protected List<String> m_PriorityJars
        optional priority jars.
      • m_EnvVars

        protected List<String> m_EnvVars
        optional environment variables.
      • m_CollapseClassPath

        protected boolean m_CollapseClassPath
        whether to collapse the classpath.
      • m_Arguments

        protected String[] m_Arguments
        the arguments for the process.
      • m_Runtime

        protected Runtime m_Runtime
        the runtime object in use.
      • m_Process

        protected Process m_Process
        the process that got launched.
      • m_DebugLevel

        protected int m_DebugLevel
        the debug level.
      • m_IgnoreEnvironmentOptions

        protected boolean m_IgnoreEnvironmentOptions
        whether to ignore the ADAMS environment options.
      • m_SuppressErrorDialog

        protected boolean m_SuppressErrorDialog
        whether to suppress error dialog.
      • m_OutputPrinter

        protected Class m_OutputPrinter
        the output printer to use.
      • m_ConsoleObject

        protected LoggingObject m_ConsoleObject
        the console object that calls the launcher (if any).
      • m_Logger

        protected Logger m_Logger
        for logging.
    • Constructor Detail

      • Launcher

        public Launcher()
        Initializes the launcher.
    • Method Detail

      • setMemory

        public String setMemory​(String value)
        Sets the amount of memory for the process.
        Parameters:
        value - the amount
        Returns:
        null if valid amount, otherwise error message
      • setDebugLevel

        public String setDebugLevel​(String value)
        Sets the debug level (0 = off, >0 on).
        Parameters:
        value - the level
        Returns:
        null if valid level, otherwise error message
      • increaseHeap

        protected void increaseHeap()
        Increase the heap by 50%.
      • setMainClass

        public String setMainClass​(String value)
        Sets the main class of the process.
        Parameters:
        value - the class name
        Returns:
        null if valid class, otherwise error message
      • setJavaAgentJar

        public String setJavaAgentJar​(String value)
        Sets the java agent jar of the process.
        Parameters:
        value - the jar
        Returns:
        null if valid class, otherwise error message
      • addJVMOption

        public void addJVMOption​(String value)
        Adds the JVM option.
        Parameters:
        value - the option
      • addClassPathAugmentations

        public void addClassPathAugmentations​(String cmdline)
        Adds the augmentations that the classpath augmenter returns.
        Parameters:
        cmdline - the classname+options of the augmenter
        See Also:
        ClassPathAugmenter
      • addEnvironmentModifier

        public void addEnvironmentModifier​(String cmdline)
        Adds the augmentations that the classpath augmenter returns.
        Parameters:
        cmdline - the classname+options of the augmenter
        See Also:
        ClassPathAugmenter
      • addPriorityJar

        public void addPriorityJar​(String value)
        Adds the priority jar.
        Parameters:
        value - the option
      • addEnvVar

        public void addEnvVar​(String value)
        Adds the environmental variable key-value pair (key=value).
        Parameters:
        value - the option
      • addClassPathAugmentations

        public void addClassPathAugmentations​(ClassPathAugmenter augmenter)
        Adds the augmentations that the classpath augmenter returns.
        Parameters:
        augmenter - the classname+options of the augmenter
        See Also:
        ClassPathAugmenter
      • collapseClassPath

        public void collapseClassPath​(boolean value)
        Sets whether to collapse the classpath (using '*' below dirs instead of explicit jar names).
        Parameters:
        value - true if to collapse
      • setArguments

        public String setArguments​(String[] value)
        Sets the arguments for the process.
        Parameters:
        value - the arguments
      • addShutdownHook

        public void addShutdownHook()
        Adds a shutdown hook, to kill the launched process.
        See Also:
        m_Process
      • ignoreEnvironmentOptions

        public void ignoreEnvironmentOptions()
        Sets to ignore the ADAMS environment options.
      • suppressErrorDialog

        public void suppressErrorDialog()
        Suppresses the error dialog.
      • setConsoleObject

        public void setConsoleObject​(LoggingObject owner)
        Sets the owning console object to use for the output printer if that implements LoggingObjectOwner.
        Parameters:
        owner - the owner for the output printer, can be null
      • getClassPath

        protected String getClassPath()
        Assembles the classpath.
        Returns:
        the full classpath
      • getStdOut

        public OutputProcessStream getStdOut()
        Returns the stdout handler.
        Returns:
        the handler
      • getStdErr

        public OutputProcessStream getStdErr()
        Returns the stderr handler.
        Returns:
        the handler
      • destroy

        public void destroy()
        Allows killing the process.
      • execute

        public String execute()
        Launches the main class.
        Returns:
        null if OK, otherwise an error message
      • configure

        protected static String configure​(String[] args,
                                          Launcher launcher)
        Configures the launcher.
        Parameters:
        args - the commandline arguments to use for launching
        launcher - the launcher to configure
      • cleanOptions

        protected static void cleanOptions​(String[] args)
        Removes unnecessary double quotes.
        Parameters:
        args - the arguments to clean
      • main

        public static void main​(String[] args)
                         throws Exception
        Executes the class from the command-line.
        Parameters:
        args - the command-line arguments
        Throws:
        Exception