Guide to Creating Jar Executables and Windows Executables from Java – 从Java创建Jar可执行文件和Windows可执行文件指南

最后修改: 2022年 7月 9日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll start by learning how to package a Java program into an executable Java ARchive (JAR) file. Then, we’ll see how to generate a Microsoft Windows-supported executable file using that executable JAR.

在本教程中,我们将首先学习如何将一个Java程序打包成一个可执行的Java ARchive(JAR)文件。然后,我们将看到如何使用该可执行的JAR文件生成一个微软Windows支持的可执行文件。

We’ll use the jar command-line tool that comes with Java for creating JAR files. We’ll then learn to use the jpackage tool, available with Java 16 and later versions as jdk.jpackage, to generate an executable file.

我们将使用Java自带的jar命令行工具来创建JAR文件。然后我们将学习使用jpackage工具,该工具在Java 16及以后的版本中以jdk.jpackage的形式提供,以生成一个可执行文件。

2. Basics of the jar and the jpackage Commands

2.jarjpackage命令的基本知识

A JAR file is a container for compiled Java class files and other resources. It’s based on the popular ZIP file format.

JAR文件是一个用于存放编译的Java类文件和其他资源的容器。它以流行的ZIP文件格式为基础。

An executable JAR file is also a JAR file but contains a main class as well. The main class is referenced in a manifest file, which we’ll discuss shortly.

一个可执行的JAR文件也是一个JAR文件,但也包含一个主类。主类在清单文件中被引用,我们很快会讨论这个问题。

In order to run an application delivered in a JAR format, we must have a Java Runtime Environment (JRE).

为了运行以JAR格式交付的应用程序,我们必须有一个Java运行时环境(JRE)。

Unlike JAR files, a platform-specific executable file can run natively on the platform it was built for. For example, that platform could be Microsoft Windows, Linux, or Apple macOS.

与JAR文件不同,特定平台的可执行文件可以在其构建的平台上自然运行。例如,该平台可能是Microsoft Windows、Linux或Apple macOS。

For a good end-user experience, it’s preferred to provide clients with a platform-specific executable file.

为了获得良好的终端用户体验,最好为客户提供一个特定平台的可执行文件

2.1. The jar Command

2.1.jar命令

The general syntax for creating a JAR file is:

创建JAR文件的一般语法是。

jar cf jar-file input-file(s)

Let’s go through some options that can be used when creating a new archive with the jar command:

让我们来看看用jar命令创建新归档文件时可以使用的一些选项。

  • c specifies that we want to create a JAR file
  • f specifies that we want the output to go to a file
  • m is used to include manifest information from an existing manifest file
  • jar-file is the name that we want for the resulting JAR file. JAR files are generally given a .jar extension, but it’s not required.
  • input-file(s) is a space-separated list of filenames that we want to include in our JAR file. The wildcard * can be used here as well.

Once we create a JAR file, we’ll often be checking its contents. To view what a JAR file contains, we use the following syntax:

一旦我们创建了一个JAR文件,我们就会经常检查其内容。要查看一个JAR文件包含的内容,我们使用以下语法。

jar tf jar-file

Here, t indicates that we want to list the contents of the JAR file. The f option denotes that the JAR file that we want to check is specified on the command line.

这里,t表示我们想列出JAR文件的内容。f选项表示我们要检查的JAR文件是在命令行中指定的。

2.2. The jpackage Command

2.2.jpackage 命令

The jpackage command-line tool helps us generate installable packages for modular and non-modular Java applications.

jpackage 命令行工具帮助我们为模块化和非模块化的Java应用程序生成可安装包

It uses the jlink command to generate a Java Runtime Image for our application.  As a result, we get a self-contained application bundle for a specific platform.

它使用jlink 命令为我们的应用程序生成一个Java Runtime Image。 因此,我们得到了一个用于特定平台的独立的应用程序包。

Since the application packages are built for a target platform, that system must contain the following:

由于应用程序包是为目标平台建立的,该系统必须包含以下内容。

  • the application itself
  • a JDK
  • a software that is needed by the packaging tool. For Windows, jpackage requires WiX 3.0 or later.

Here’s the commonly-used form of the jpackage command:

下面是jpackage 命令的常用形式。

jpackage --input . --main-jar MyAppn.jar

jpackage --input .--main-jar MyAppn.jar

3. Creating Executable Files

3.创建可执行文件

Now let’s go through creating an executable JAR file. Once that’s ready, we’ll work on generating a Windows executable file.

现在让我们去创建一个可执行的JAR文件。一旦准备好了,我们将致力于生成一个Windows可执行文件。

3.1. Creating an Executable JAR File

3.1.创建一个可执行的JAR文件

Creating an executable JAR is fairly simple. We’ll first need a Java project with at least one class with the main() method. We created a Java class named MySampleGUIAppn for our example.

创建一个可执行的JAR是相当简单的。我们首先需要一个Java项目,其中至少有一个带有main()方法的类。我们为我们的例子创建了一个名为MySampleGUIAppn的Java类。

The second step is to create a manifest file. Let’s create our manifest file as MySampleGUIAppn.mf:

第二步是创建一个清单文件。让我们将清单文件创建为MySampleGUIAppn.mf

Manifest-Version: 1.0
Main-Class: MySampleGUIAppn

We have to make sure there’s a newline at the end of this manifest file for it to work correctly.

我们必须确保在这个清单文件的结尾有一个换行,以便它能正常工作。

Once the manifest file’s ready, we’ll create an executable JAR:

一旦清单文件准备就绪,我们将创建一个可执行的JAR。

jar cmf MySampleGUIAppn.mf MySampleGUIAppn.jar MySampleGUIAppn.class MySampleGUIAppn.java

Let’s view the contents of the JAR that we created:

让我们查看一下我们创建的JAR的内容。

jar tf MySampleGUIAppn.jar

Here’s a sample output:

下面是一个输出样本。

META-INF/
META-INF/MANIFEST.MF
MySampleGUIAppn.class
MySampleGUIAppn.java

Next, we can run our JAR executable via a CLI or in a GUI.

接下来,我们可以通过CLI或在GUI中运行我们的JAR可执行文件。

Let’s run it on the command line:

让我们在命令行上运行它。

java -jar MySampleGUIAppn.jar

In a GUI, we can simply double-click the relevant JAR file. That should launch it normally as any other application.

在GUI中,我们可以简单地双击相关的JAR文件。这应该像其他应用程序一样正常启动它。

3.2. Creating a Windows Executable

3.2.创建一个Windows可执行文件

Now that our executable JAR is ready and working, let’s generate a Windows executable file for our sample project:

现在,我们的可执行JAR已经准备好了,让我们为我们的样本项目生成一个Windows可执行文件。

jpackage --input . --main-jar MySampleGUIAppn.jar

This command takes a short while to complete. Once completed, it produces an exe file in the current working folder. The executable’s file name will be concatenated with the version number mentioned in the manifest file.  We’ll be able to launch it just like any other Windows application.

这个命令需要很短的时间来完成。一旦完成,它会在当前工作文件夹中生成一个exe文件。该可执行文件的文件名将与清单文件中提到的版本号相连接。 我们将能够像其他Windows应用程序一样启动它。

Here are some more Windows-specific options that we can use with the jpackage command:

下面是一些我们可以在jpackage命令中使用的更多Windows专用选项。

  • –type: to specify msi instead of the default exe format
  • –win-console: to start our application with a console window
  • –win-shortcut: to create a short-cut file in the Windows Start menu
  • –win-dir-chooser: to let an end-user specify a custom directory to install the executable
  • –win-menu –win-menu-group: to let an end-user specify a custom directory in the Start menu

4. Conclusion

4.总结

In this article, we learned some basics about JAR files and executable JAR files. We also saw how to convert a Java program into a JAR executable, and later into a Microsoft Windows-supported executable file.

在这篇文章中,我们学习了一些关于JAR文件和可执行JAR文件的基本知识。我们还看到了如何将一个Java程序转换成JAR可执行文件,然后再转换成微软Windows支持的可执行文件。

As always, the source code for the examples is available over on GitHub.

像往常一样,这些例子的源代码可以在GitHub上找到