Guide to Creating and Running a Jar File in Java – 在Java中创建和运行Jar文件的指南

最后修改: 2019年 2月 7日

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

1. Overview

1.概述

Usually, it’s convenient to bundle many Java class files into a single archive file.

通常情况下,将许多Java类文件捆绑到一个存档文件中是很方便的。

In this tutorial, we’re going to cover the ins and outs of working with jar – or Java ARchive – files in Java. 

在本教程中,我们将介绍在Java中使用jar–或JavaARchive–文件的内涵和外延。

Specifically, we’ll take a simple application and explore different ways to package and run it as a jar. We’ll also answer some curiosities like how to easily read a jar’s manifest file along the way.

具体来说,我们将采取一个简单的应用程序,并探索将其打包并作为jar运行的不同方法。我们还将回答一些好奇的问题,比如如何轻松读取jar的清单文件

2. Java Program Setup

2.Java程序设置

Before we can create a runnable jar file, our application needs to have a class with a main method. This class provides our entry point into the application:

在创建可运行的jar文件之前,我们的应用程序需要有一个带有main方法的类。这个类提供了我们进入应用程序的入口。

public static void main(String[] args) {
    System.out.println("Hello Baeldung Reader!");
}

3. Jar Command

3. jar命令

Now that we’re all set up let’s compile our code and create our jar file.

现在我们都准备好了,让我们来编译我们的代码并创建我们的jar文件。

We can do this with javac from the command line:

我们可以用命令行中的javac来做这个。

javac com/baeldung/jar/*.java

The javac command creates JarExample.class in the com/baeldung/jar directory. We can now package that into a jar file.

javac命令在com/baeldung/jar目录下创建JarExample.class。我们现在可以把它打包成一个jar文件。

3.1. Using the Defaults

3.1.使用默认值

To create the jar file, we are going to use the jar command.

为了创建jar文件,我们将使用jar命令。

To use the jar command to create a jar file, we need to use the c option to indicate that we’re creating a file and the f option to specify the file:

要使用jar命令来创建一个jar文件,我们需要使用c选项来表示我们正在创建一个文件,并使用f选项来指定文件。

jar cf JarExample.jar com/baeldung/jar/*.class

3.2. Setting the Main Class

3.2.设置主类

It’s helpful for the jar file manifest to include the main class.

这对jar文件清单包括主类是有帮助的。

The manifest is a special file in a jar located the META-INF directory and named MANIFEST.MFThe manifest file contains special meta information about files within the jar file.

清单是位于META-INF目录的jar中的一个特殊文件,名为MANIFEST.MF清单文件包含关于jar文件中的文件的特殊元信息。

Some examples of what we can use a manifest file for include setting the entry point, setting version information and configuring the classpath.

我们可以使用清单文件的一些例子包括设置入口点、设置版本信息和配置classpath。

By using the e option, we can specify our entry point, and the jar command will add it to the generated manifest file.

通过使用e选项,我们可以指定我们的入口点,而jar命令将把它添加到生成的清单文件中。

Let’s run jar with an entry point specified:

让我们运行jar,指定一个入口点。

jar cfe JarExample.jar com.baeldung.jar.JarExample com/baeldung/jar/*.class

3.3. Updating the Contents

3.3.更新内容

Let’s say we’ve made a change to one of our classes and recompiled it. Now, we need to update our jar file.

假设我们对我们的一个类进行了修改,并重新编译了它。现在,我们需要更新我们的jar文件。

Let’s use the jar command with the option to update its contents:

让我们使用带有u选项的jar命令来更新其内容。

jar uf JarExample.jar com/baeldung/jar/JarExample.class

3.4. Setting a Manifest File

3.4.设置一个清单文件

In some cases, we may need to have more control over what goes in our manifest file. The jar command provides functionality for providing our own manifest information.

在某些情况下,我们可能需要对清单文件中的内容进行更多控制。jar命令提供了提供我们自己的清单信息的功能。

Let’s add a partial manifest file named example_manifest.txt to our application to set our entry point:

让我们为我们的应用程序添加一个名为example_manifest.txt的部分清单文件,以设置我们的入口点。

Main-Class: com.baeldung.jar.JarExample

The manifest information we provide we’ll be added to what the jar command generates, so it’s the only line we need in the file.

我们提供的清单信息将被添加到jar命令生成的内容中,所以这是我们在文件中唯一需要的一行。

It’s important that we end our manifest file with a newline. Without the newline, our manifest file will be silently ignored.

我们必须用换行来结束我们的清单文件。没有换行,我们的清单文件将被默默地忽略。

With that setup, let’s create our jar again using our manifest information and the option:

有了这些设置,让我们使用清单信息和m选项再次创建我们的jar。

jar cfm JarExample.jar com/baeldung/jar/example_manifest.txt com/baeldung/jar/*.class

3.5. Verbose Output

3.5.冗长的输出

If we want more information out of the jar command, we can simply add the v option for verbose.

如果我们想从jar命令中得到更多的信息,我们可以简单地添加v选项,表示verbose。

Let’s run our jar command with the v option:

让我们用v选项运行我们的jar命令。

jar cvfm JarExample.jar com/baeldung/jar/example_manifest.txt com/baeldung/jar/*.class
added manifest
adding: com/baeldung/jar/JarExample.class(in = 453) (out= 312)(deflated 31%)

4. Using Maven

4.使用Maven

4.1. Default Configuration

4.1.默认配置

We can also use Maven to create our jar. Since Maven favors convention over configuration, we can just run package to create our jar file.

我们也可以用Maven来创建我们的jar。由于Maven更倾向于惯例而不是配置,我们可以直接运行package来创建jar文件。

mvn package

By default, our jar file will be added to the target folder in our project.

默认情况下,我们的jar文件将被添加到我们项目的target文件夹中。

4.2. Indicating the Main Class

4.2.表明主类

We can also configure Maven to specify the main class and create an executable jar file.

我们还可以配置Maven来指定主类,并创建一个可执行jar文件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>${maven-jar-plugin.version}</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.baeldung.jar.JarExample</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

5. Using Spring Boot

5.使用Spring Boot

5.1. Using Maven and Defaults

5.1.使用Maven和默认值

If we’re using Spring Boot with Maven, we should first confirm that our packaging setting is set to jar rather than war in our pom.xml file.

如果我们使用带有Maven的Spring Boot,首先应该确认我们的打包设置是jar,而不是war,在pom.xml文件中。

<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot</artifactId>
<packaging>jar</packaging>
<name>spring-boot</name>

Once we know that’s configured, we can run the package goal:

一旦我们知道那是配置好的,我们就可以运行目标。

mvn package

5.2. Setting the Entry Point

5.2.设置入口点

Setting our main class is where we find differences between creating a jar with a regular Java application and a fat jar for a Spring Boot application. In a Spring Boot application, the main class is actually org.springframework.boot.loader.JarLauncher.

设置我们的主类是我们发现用普通的Java应用程序创建jar和为Spring Boot应用程序创建fat jar之间的区别。在Spring Boot应用程序中,主类实际上是org.springframework.boot.Loader.JarLauncher

Although our example isn’t a Spring Boot application, we could easily set it up to be a Spring Boot console application.

虽然我们的例子不是Spring Boot应用程序,但我们可以轻松地将其设置为Spring Boot控制台应用程序

Our main class should be specified as the start class:

我们的主类应该被指定为起始类。

<properties>
    <start-class>com.baeldung.jar.JarExample</start-class>
    <!-- Other properties -->
</properties>

We can also use Gradle to create a Spring Boot fat jar.

我们还可以使用Gradle来创建一个Spring Boot fat jar

6. Running the Jar

6.运行罐子

Now that we’ve got our jar file, we can run it. We run jar files using the java command.

现在我们已经得到了我们的jar文件,我们可以运行它。我们使用java命令来运行jar文件

6.1. Inferring the Main Class

6.1.推断主类

Since we’ve gone ahead and made sure our main class is specified in the manifest, we can use the -jar option of the java command to run our application without specifying the main class:

由于我们已经提前确保在清单中指定了我们的主类,我们可以使用java命令的-jar选项来运行我们的应用程序,而无需指定主类:

java -jar JarExample.jar

6.2. Specifying the Main Class

6.2.指定主类

We can also specify the main class when we’re running our application. We can use the -cp option to ensure that our jar file is in the classpath and then provide our main class in the package.className format:

我们也可以在运行应用程序时指定主类。我们可以使用-cp选项来确保我们的jar文件在classpath中,然后以package.className格式提供我们的主类。

java -cp JarExample.jar com.baeldung.jar.JarExample

Using path separators instead of package format also works:

使用路径分隔符而不是包的格式也可以。

java -cp JarExample.jar com/baeldung/jar/JarExample

6.3. Listing the Contents of a Jar

6.3.列出一个罐子的内容

We can use the jar command to list the contents of our jar file:

我们可以使用jar命令来列出我们jar文件的内容。

jar tf JarExample.jar
META-INF/
META-INF/MANIFEST.MF
com/baeldung/jar/JarExample.class

6.4. Viewing the Manifest File

6.4.查看清单文件

Since it can be important to know what’s in our MANIFEST.MF file, let’s look at a quick and easy way we can peek at the contents without leaving the command line.

由于知道我们的MANIFEST.MF文件中的内容可能很重要,让我们看看一个快速而简单的方法,我们可以在不离开命令行的情况下偷看其内容。

Let’s use the unzip command with the -p option:

让我们使用带有-p选项的unzip命令。

unzip -p JarExample.jar META-INF/MANIFEST.MF
Manifest-Version: 1.0
Created-By: 1.8.0_31 (Oracle Corporation)
Main-Class: com.baeldung.jar.JarExample

7. Conclusion

7.结语

In this tutorial, we set up a simple Java application with a main class.

在本教程中,我们设置了一个简单的Java应用程序,其中有一个main类。

Then we looked at three ways of creating jar files: using the jar command, with Maven and with a Maven Spring Boot application.

然后我们研究了创建jar文件的三种方式:使用jar命令、使用Maven和使用Maven Spring Boot应用程序。

After we created our jar files, we returned to the command line and ran them with an inferred and a specified main class.

在我们创建了jar文件后,我们回到命令行,用一个推断的和指定的主类运行它们。

We also learned how to display the contents of a file and how to display the contents of a single file within a jar.

我们还学习了如何显示一个文件的内容,以及如何显示一个jar中的单个文件的内容。

Both the plain Java example and the Spring Boot example are available over on GitHub.

普通的Java 示例Spring Boot 示例都可以在 GitHub 上找到。