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

最后修改: 2022年 7月 9日

1. Overview


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


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


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.


In order to run an application delivered in a JAR format, we must have a Java Runtime Environment (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


The general syntax for creating a JAR file is:


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:


  • 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 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.


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


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


3.1. Creating an Executable JAR File


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.


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


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 cmf MySampleGUIAppn.jar MySampleGUIAppn.class

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


jar tf MySampleGUIAppn.jar

Here’s a sample output:



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


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.


3.2. Creating a Windows Executable


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


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:


  • –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


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.


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