Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

AppLauncher



The AppLauncher class launch a process to start a Java jar file.

The supported arguments are:
  • The location of the JavaFX libraries if they are required
  • The libraries path
  • The module paths (the --add-modules declarations)
  • The --add-opens declarations
  • The --add-exports declarations
  • The required Java version
  • Environment variables
  • Specific additional VM arguments

This class and all its dependencies is on the AppLauncher.jar package. It has no dependencies on other packages.

Overview

Main Article: AppLauncher use cases

The class allows to start a new process with a specified JVM (not necessarily the current JVM used to execute the AppLauncher program) with customized arguments specified in a configuration file:
applauncher
Note that the launched application which is the target of the launcher will be started in a new child process, and with a new JVM (specified in the configuration), thus even if the location of the JVM is the same than the one which is used by the AppLauncher program.


You need to extend the AppLauncher class because the AppLauncher class is an abstract class.

Extend the AppLauncher class

Mandatory methods


To extend the AppLauncher class, you need to implement the two following methods:
  • The AppLauncher.getLaunchable() method must return the name of the jar file which contains the application to start. Note that this file must be in the same directory as the jar file which contains the launcher class
  • The AppLauncher.getConfigurationFile() method must return the name of the configuration file which will be used to configure the application. This file must also be in the same directory as the jar file which contains the AppLauncher class
You also need to add a static main(String[]) method to create the launcher and call the AppLauncher.start() method which will start your application in a child process.

For example, suppose that you want to start the MyApplication.jar application with the MyAppLauncher.jar launcher. The file structure must be:
applauncherstruct
The code of the MyAppLauncher.jar class should be:
   public class MyAppLauncher extends AppLauncher {
      public AppLauncher(String[] args) {
        super(args);
      }  
      
      public static void main(String[] args) {
        MyAppLauncher launcher = new MyAppLauncher(args);
        launcher.start();
      }      
          
      @Override
      protected String getLaunchable() {
        return "MyApplication.jar";
      }

      @Override
      protected String getConfigurationFile() {
        return "confFile.conf";
      }
   }

Optional methods

Several optional methods allow to:
  • Specify the module paths (the --add-modules declaration)
  • Specify the --add-opens declarations
  • Specify the --add-exports declarations
  • Specify the required Java version
  • Specify the allowed custom properties
  • Specify the path of the main Class

Specify the module paths

The AppLauncher.setAddModules(String) method allows to set the --add-modules declaration[1]
See dev.java for more information
.

For example:
   launcher.setAddModules("javafx.controls,javafx.fxml,javafx.graphics,javafx.swing,javafx.web");

This will not be used if the used JVM is less than Java 9.

Specify the --add-opens declarations

The AppLauncher.setAddOpensAllUnnamed(String...) method allows to set the ---add-opens declarations with an ALL-UNNAMED value[2]
See dev.java for more information
.

For example:
   launcher.setAddOpensAllUnnamed("javafx.web/javafx.scene.web", "javafx.web/com.sun.webkit");

This will not be used if the used JVM is less than Java 9.

Specify the --add-exports declarations

The AppLauncher.setAddExportsAllUnnamed(String...) method allows to set the ---add-exports declarations with an ALL-UNNAMED value[3]
See dev.java for more information
.

For example:
   launcher.setAddExportsAllUnnamed("javafx.web/javafx.scene.web", "javafx.web/com.sun.webkit");

This will not be used if the used JVM is less than Java 9.

Specify the required Java version

The AppLauncher.requireJavaVersion(String) method allows to specify a required JVM major version to execute your program. There are several ways to declare the version:
  • If the argument has the {number} pattern, then the associated major version is required
  • If the argument has the {number}- pattern, then the major version must be greater or equal to the associated number
  • If the argument has the -{number} pattern, then the major version must be smaller or equal to the associated number
  • If the argument has the {number}-{number} pattern, then the major version must be between the two associated numbers
For example:
   launcher.requireJavaVersion("11"); the major version must be 11
   launcher.requireJavaVersion("11-"); the major version must be 11 or more
   launcher.requireJavaVersion("-11"); the major version must be 11 or less
   launcher.requireJavaVersion("9-11"); the major version must be between 9 and 11

Specify the allowed custom properties

The AppLauncher.getCustomProperties() method returns the allowed set of custom properties names, which will be used in the configuration properties file. By

The AppLauncher.separateCustomArguments(boolean) specifies how the custiom properties names and values are handled in the arguments:
  • By default the key and the values are concatenated with a "=" sign
  • If the parameter of the "separateCustomArguments method is true, then the key and value will be on two separate arguments
For example:
  • By default myKey=myValue in the configuration file will create in the arguments myKey=myValue
  • With the true value for the method myKey=myValue in the configuration file will create in the arguments myKey myValue

By default the method returns null, which means that no custom properties are allowed.

Specify the main Class

The AppLauncher.setMainClass(String) method allows to specify the path of the main Class to use when starting the application.

For example:
  launcher.setMainClass("my.main.MyApplication");

Specify the path of the configuration file as an argument

Main Article: Mandatory methods

The AppLauncher.getConfigurationFile() method return the name of the configuration file which will be used to configure the application. This file must be in the same directory as the jar file which contains the launcher class.

It is also possible to allow the user to specify the path of the file by using the -appLauncher.conf=>path> argument when you start the launcher. The path can be absolute or relative to the directory of the launcher.

For example:
      java -jar AppLauncher.jar -appLauncher.conf=myConfigFile.conf
If the path is empty, a file chooser will ask the user to select the file. For example:
      java -jar AppLauncher.jar -appLauncher.conf=

If the file specified in the argument does not exist, or the user cancels the dialog, the default file specified by the AppLauncher.getConfigurationFile() method will be used.

Configuration file properties

The file properties has several properties which allows to:
  • JAVAHOME: Specifies the location of the JVM to use for the application to launch
  • JAVAFXLIB: Specifies the location of the JavaFX libraries for use to the application to launch
  • MODULEPATH: Specifies the "--module-path" argument. This is an alternate way of setting the "--module-path" through the API (see Specify the module paths)
  • LIBPATH: Specifies the "java.library.path" argument
  • VMARGS: Specifies additional VM arguments to use
  • REQUIRE_VERSION: Specifies the required JVM version. This is an alternate way of setting it through the API (see Specify the required Java version)
  • ENV:<environment variable name>: environment variables that you want to set for the application process
  • Custom properties: all properties with names different from the previous ones

JAVAHOME property

The JAVAHOME property specifies the location of the JVM to use for the application to launch.

If this property is empty or it does not exists, then the default JVM will be used.


For example:
   JAVAHOME=L:\WRK\Java\Tools\jdk-17.0.2

JAVAFXLIB property

The JAVAFXLIB property specifies the location of the JavaFX libraries for use to the application to launch.

This property is not used (even if it is specified) for a JVM which is less than Java 11, because before Java 11, JavaFX was bundled with the JVM installation.


For example:
   JAVAFXLIB=L:\WRK\Java\Tools\javafx17\lib

MODULEPATH property

The MODULEPATH property specifies the "--module-path" argument. This is an alternate way of setting the "--module-path" through the API (see Specify the module paths).

This property is not used (even if it is specified) for a JVM which is less than Java 9, because the module system is only available beginning with Java 9.


For example:
   MODULEPATH=javafx.controls,javafx.fxml,javafx.graphics,javafx.swing,javafx.web

LIBPATH property

The LIBPATH property specifies the java.library.path argument.

VMARGS property

The VMARGS property additional VM arguments to use. The arguments must be separated by spaces.

REQUIRE_VERSION property

The REQUIRE_VERSION property specifies the required JVM version. This is an alternate way of setting it through the API (see Specify the required Java version).

Environment variables properties

The ENV:<environment variable name> properties specify environment variables that you want to set for the application process.

For example:
   ENV:HOMEPAGE=http://my.homepage.html

Custom property

All properties with names different from the previous ones are custom properties and will be added in the arguments (for those which names are allowed).

For example:
  JAVAHOME=L:\WRK\Java\Tools\jdk-17.0.2
  myKey=myValue

Custom properties are only allowed if the specify the allowed custom properties is set.

Start the application

The AppLauncher.start() method creates a process to start the application.

Handle errors

By default errors will throw a AppLauncherException. The kinds of errors are: It is possible to specify other ways to handle errors during the class creation:

Notes

  1. ^ See dev.java for more information
  2. ^ See dev.java for more information
  3. ^ See dev.java for more information

See also


Categories: lang.applauncher | packages

Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences