Java 简明教程

Java - Packaging Tools

在 Java 14 中,引入了新的 packaging tool jpackage 以替换 javapackager。javapackager 是 JavaFX 工具包的一部分,并于 Java 8 中引入。作为 Java 11 起,JavaFX 并非 Java API 的标准功能,javapackager 不是标准提供的一部分。jpackage 实现了此目的。jpackage 使开发人员能够将 jar 文件打包成本机可安装格式,如 Windows 的 exe/msi、MacOS 的 pkg/dmg 等。

In Java 14, a new packaging tool, jpackage was introduced to replace javapackager. javapackager was part of JavaFX kit and was introduced in Java 8. As Java 11 onwards, JavaFX is not a standard feature of Java API, javapackager is not a part of the standard offering. jpackage serves this purpose. jpackage enables developers to package the jar file in a native installable format like exe/msi for Windows, pkg/dmg for MacOS and so on.

开发人员可以使用 jlink 将所需的 JDK 模块压缩到最少限度的模块,并使用 jpackage 创建一个轻量级映像。

Developer can use jlink to compress the required JDK modules to minimum modules and use the jpackage to create a lightweight image.

Need of jpackager

当需要分发软件时,首选方式是向最终用户提供一个可安装的软件包。该可安装软件包通常包含 JDK、模块、任何依赖文件和配置,并为用户提供熟悉的方式来安装该软件。用户不应被要求将 JRE 或 JDK 作为运行 Java 应用程序的先决条件。jpackage 处理所有这些情况,并将所有必需的文件与 JRE/JDK 一起打包到本机安装程序中。

When software is to be distributed, the preferred way is to deliver an installable package to the end-user. This installable package generally contains the JDK, the modules, any dependent files, and configurations and provides the user with a familiar way to install the software. Users should not be required to install JRE or JDK as a prerequisite to run the Java application. jpackage handles all these cases and packages all the required files along with JRE/JDK into a native installer.

Command Line Options for jpackager

jpackage 是一个命令行工具,提供了各种选项来定制可安装的软件。以下是一些 jpackager 提供的功能:

jpackage is a command line tool and provides various options to customize installable softwares. Following are some of the features that jpackager provides:

  1. Developer can provide a custom icon.

  2. Developer can provide a specific location to install the application.

  3. Developer can application arguments to be passed to the application, JVM options to be used while launching the application.

  4. Developer can set file associations to launch the application.

  5. Developer can set option to modify platform-specific menu group options to launch the application.

  6. Developer can configure multiple launchers to launch the application.

  7. Using XCode, a bundle can be signed as well. This is applicable for MacOS only though.

Prerequisite

以下是在准备可安装文件之前使用 jpackager 工具的先决条件。

Folowing are the prerequisite to use jpackager tool to prepare a installable.

  1. First requirement is to have the JDK and software application.

  2. Get platform specific packaging tool as specified below: Windows− To create EXE/MSI Installable, a third party library wix 3.0 or later is required Ubuntu Linux− To create a RPM, DEB package, we need fakeroot package. Red Hat Linux− To create a RPM, DEB package, we require rpm-build package. MacOS− We can create package using Xcode command line tools. -mac-sign option can be used to sign the package and -icon can be used to provide a customized icon.

  3. Application package should be prepared as per the platform. For each platform, we’ve to run the command separately.

Create a Package

我们可以使用以下命令创建软件包:

We can create the package using following command:

Syntax

jpackage --input lib \
  --name Tester \
  --main-jar Tester.jar \
  --main-class com.tutorialspoint.Tester \
  --type msi \
  --java-options '--enable-preview'

其中

Where

  1. input − folder containing the required libraries.

  2. name − name of the installable package

  3. main-jar − jar file to be launched to start the application.

  4. main-class − name of the main class in the JAR to be launched. If MANIFEST.MF file in the main JAR contains the main class name then this option is not required.

  5. type − type of installable. DMG/PKG for MacOS, MSI/EXE options on Windows and DEB/RPM options on Linux.

  6. java-options − options for the Java runtime

此命令将创建一个可在 Windows 上安装的 MSI 文件,并且应用程序可以使用任何其他软件。

This command will create a MSI file which can be installed on Windows and application can be used like any other software.

Example of a Package

public class APITester {
   public static void main(String[] args) {
      System.out.println("Welcome to TutorialsPoint.");
   }
}

编译和运行程序

Compile and Run the program

$javac APITester.java
$jar cf APITester.jar APITester.class

Output

对于 Windows 可执行文件,需要下载 WiX Toolset v3.11.2(wix311-binaries.zip) 并将工具包添加到路径。

For windows executable, you need to download WiX Toolset v3.11.2(wix311-binaries.zip) and add the toolkit to your path.

创建 jar 并设置路径后,将 jar 放入名为 lib 的文件夹中,并运行以下命令来创建一个 Windows MSI 安装程序。

Once jar is created and path is set, put jar in a folder called lib and run the following command to create a windows MSI installer.

$jpackage --input lib --name APITester --main-jar APITester.jar --main-class APITester --type msi