Standalone Java Embedded Applications

I watched several online presentations that briefly mentioned standalone applications in Java 9, but I have a question that I would like to clarify.

Thanks to the new modular system, you are now allowed to include only the minimum amount of code needed to run your application. However, does the system that wants to run the application require a JRE, or is it something that can be included in the base module in the program?

I suspect the latter, since the page ( here ) to download the latest version of Java still displays version 8_151.

TL; DR - Using Java 9, is it possible to create a standalone executable file that can run on a system without JRE / Java installed?

+10
java java-9 self-contained java-module
source share
2 answers

Jlink

Yes, this is possible using jlink ( JEP 282 ), but all your code and your dependencies should be modular JAR module-info.class (i.e. module-info.class With module-info.class ). It works like this:

 jlink --module-path $JAVA_HOME/jmods:mods --add-modules your.app --launcher launch-app=your.app --output your-app-image 

In details:

  • --module-path lists the folders that contain the modules - this should include the platform modules that come with the JDK you want to use (in $JAVA_HOME/jmods ) and your application modules ( mods )
  • --add-modules names --add-modules that you want your runtime image to contain - all of its transitive dependencies are included
  • --launcher optional, but very convenient; it creates an OS-specific launchpad (e.g. .bat on Windows) with a given name ( launch-app ) that launches the specified module ( your.app ; in this case, if the main class is defined for it)
  • --output indicates where to create the runtime image
+10
source share

javapackager

Alternatively, you can use the javapackager tool ( JEP 343 ).

Java packaging tools provide native support for several formats of stand-alone application packages.

The basic package is a single folder on your hard drive, which includes all the application resources and JRE. You can distribute the package as is, or you can create an installable package (for example, in EXE or DMG format).

Although there are certain limitations associated with the javapackager these applications using javapackager which includes:

  • Standalone application packages should be explicitly requested by passing their own argument to the Ant task or the javapackager -deploy command.

  • Stand-alone application packages must be built in the operating system in which they are intended. The necessary tools must be available to build the package in a specific format.

  • Standalone application packages can only be built using JDK 7 Update 6 or later. The Java Packager for JDK 9 packs applications with the JDK 9 runtime. To pack the JRE JDK 8 or JDK 7 with your application, use the Java Packager JDK 8.


One way to create a basic stand-alone application is to modify the deploy ant task:

 <fx:deploy width="${javafx.run.width}" height="${javafx.run.height}" nativeBundles="all" outdir="${basedir}/${dist.dir}" outfile="${application.title}"> <fx:application name="${application.title}" mainClass="${javafx.main.class}"/> <fx:resources> <fx:fileset dir="${basedir}/${dist.dir}" includes="*.jar"/> </fx:resources> <fx:info title="${application.title}" vendor="${application.vendor}"/> </fx:deploy> 

Native packages can be javapackager command tool javapackager . The Java Packager command for generating standalone application packages will look something like this:

 javapackager -deploy -native -outdir OUTPUT_DIR -outfile APPLICATION_NAME -srcdir PACKAGE_SRC_DIR -srcfiles APPLICATION.jar -appclass MAIN_CLASS -name "YourApplication" -title "SelfContained" 
+7
source share

All Articles