Java Packaging Tools: Introduction to jpackage
Discover the new Java packaging tool, jpackage
, introduced in Java 14 to replace the outdated javapackager
. This tool enables developers to create native installers for their Java applications, simplifying distribution and installation across platforms like Windows, macOS, and more.
Java - Packaging Tools
In Java 14, a new packaging tool called jpackage
was introduced to replace javapackager
. The javapackager
was part of the JavaFX kit and was introduced in Java 8. Starting from Java 11, JavaFX is no longer a standard feature of the Java API, so javapackager
is not included in the standard offering. The jpackage
tool serves this purpose by allowing developers to package their JAR files in a native installable format such as exe
/msi
for Windows, pkg
/dmg
for macOS, and more.
Need for jpackage
When software is distributed, the preferred method is to deliver an installable package to the end user. This installable package typically includes the JDK, required modules, dependent files, and configurations, providing users with a familiar installation process. Users should not need to install the JRE or JDK to run the Java application. The jpackage
tool manages all these requirements and packages everything needed, including the JRE/JDK, into a native installer.
Command Line Options for jpackage
The jpackage
tool is a command-line utility that offers various options to customize installable software. Here are some features provided by jpackage
:
- Developers can provide a custom icon.
- Specify a location to install the application.
- Pass application arguments and JVM options during application launch.
- Set file associations to launch the application.
- Modify platform-specific menu group options to launch the application.
- Configure multiple launchers for the application.
- On macOS, bundles can be signed using Xcode.
Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.
Prerequisites
To use the jpackage
tool for creating an installable package, the following prerequisites must be met:
- You must have the JDK and your software application ready.
- Obtain the platform-specific packaging tools as specified below:
- Windows: To create EXE/MSI installables, the third-party library WiX 3.0 or later is required.
- Ubuntu Linux: To create RPM and DEB packages, the
fakeroot
package is needed. - Red Hat Linux: To create RPM and DEB packages, the
rpm-build
package is required. - macOS: Use Xcode command line tools to create packages. The
-mac-sign
option can be used to sign the package, and the-icon
option can provide a custom icon. - Prepare your application package according to the platform requirements. Commands must be run separately for each platform.
Create a Package
You can create a package using the following command:
Syntax
jpackage --input lib \
--name Tester \
--main-jar Tester.jar \
--main-class com.tutorialsarena.Tester \
--type msi \
--java-options '--enable-preview'
Output
This command will create an MSI file which can be installed on Windows, allowing the application to be used like any other software.
Example of a Package
Here’s a simple example:
Java Class Example
public class APITester {
public static void main(String[] args) {
System.out.println("Welcome to tutorialsarena.");
}
}
Output
Welcome to tutorialsarena.
To compile and run the program, execute the following commands:
Commands
$ javac APITester.java
$ jar cf APITester.jar APITester.class
Creating the Installer
For Windows executable, download WiX Toolset v3.11.2 (wix311-binaries.zip) and add the toolkit to your path. After creating the JAR file and setting the path, place the JAR in a folder named lib
and run the following command to create a Windows MSI installer:
Command to Create Installer
$ jpackage --input lib --name APITester --main-jar APITester.jar --main-class APITester --type msi