I've watched a few online presentations that briefly mentioned self-contained applications in Java 9, but I have a question that I would like cleared up.
With the new module system, you're now allowed to only include the minimum amount of code required to run your application. However, does the system that wishes to run the application still require the JRE, or is that something that can be included in the base module within the program?
I suspect it's the latter, as the page (here) to download the newest version of Java still shows version 8_151.
TL;DR - Using Java 9, is it possible to create a self-contained executable that can be executed on a system without the JRE/Java installed?
A self-contained application consists of a single, installable bundle that contains your application and a copy of the JRE needed to run the application. When the application is installed, it behaves the in the same way as any native application.
The Java application package that is generated by default includes the following items: Executable application JAR file, which contains application code and resources and can be launched by double-clicking the file. Additional application JAR and resource files.
Most Java software nowadays runs only on servers (web servers or app servers). They are typically deployed as WAR or EAR files, which are also ZIP archives containing classes and other resources. These applications then run inside a server component following the Servlet or EJB standards.
Yes, this is possible with jlink
(JEP 282), but all of your code and your dependencies need to be modular JARs (i.e. ones 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 detail:
--module-path
lists the folders that contain modules - this needs to include the platform modules shipped with the JDK you want to use (in $JAVA_HOME/jmods
) and your application modules (mods
)--add-modules
names the module(s) that you want your runtime image to contain - all of its (their) transitive dependencies are included--launcher
is optional, but very handy; it creates an OS-specific launcher (e.g. a .bat
on Windows) with the given name (launch-app
) that launches the specified module (your.app
; in this case assuming the main class is defined for it)--output
specifies where to create the runtime imageAs an edit (pointed in comments), javapackager
was removed from JDK in Java-10 and one can look forward to making use of jpackage
as an incubating tool since Java-14.
You can use javapackager
tool.
The Java packaging tools provide built-in support for several formats of self-contained application packages.
The basic package is a single folder on your hard drive that includes all application resources and the JRE. The package can be redistributed as is, or you can build an installable package (for example, EXE or DMG format.)
Though there are certain restrictions associated with building these applications with javapackager
which includes -
Self-contained application packages must be explicitly requested by passing the native argument to the Ant task or javapackager -deploy command.
Self-contained application packages must be built on the operating system on which it is intended to run. Prerequisite tools must be available to build a package in a specific format.
Self-contained application packages can only be built using JDK 7 Update 6 or later. The Java Packager for JDK 9 packages applications with a JDK 9 runtime image. To package a JDK 8 or JDK 7 JRE with your application, use the JDK 8 Java Packager.
One way to create a basic self-contained 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 built using the javapackager
command tool. Java Packager Command to Generate Self-Contained Application Packages would be something like -
javapackager -deploy -native -outdir OUTPUT_DIR -outfile APPLICATION_NAME
-srcdir PACKAGE_SRC_DIR -srcfiles APPLICATION.jar -appclass MAIN_CLASS
-name "YourApplication" -title "SelfContained"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With