Importance of Main Manifest Attribute in a Self-Executing JAR – 自执行JAR中的主宣言属性的重要性

最后修改: 2018年 6月 28日

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

1. Overview

1.概述

Every executable Java class has to contain a main method. Simply put, this method is a starting point of an application.

每个可执行的Java类都必须包含一个main方法。简单地说,这个方法是一个应用程序的起点。

To run our main method from a self-executing JAR file, we have to create a proper manifest file and pack it along with our code. This manifest file has to have a main manifest attribute that defines the path to the class containing our main method.

要从自执行的 JAR 文件中运行我们的主方法,我们必须创建一个适当的清单文件,并将其与我们的代码一起打包。该清单文件必须有一个main manifest属性,该属性定义了包含主方法的类的路径。

In this tutorial, we’ll show how to pack a simple Java class as a self-executing JAR and demonstrate the importance of a main manifest attribute for a successful execution.

在本教程中,我们将展示如何将一个简单的Java类打包成一个自执行的JAR,并演示主清单属性对成功执行的重要性。

2. Executing a JAR Without the Main Manifest Attribute

2.执行一个没有主manifest属性的JAR

To get more practical, we’ll show an example of unsuccessful execution without the proper manifest attribute.

为了更加实用,我们将展示一个没有适当的清单属性而执行不成功的例子。

Let’s write a simple Java class with a main method:

让我们写一个有主方法的简单Java类。

public class AppExample {
    public static void main(String[] args){
        System.out.println("AppExample executed!");
    }
}

To pack our example class to a JAR archive, we have to go to the shell of our operating system and compile it:

为了把我们的例子类打包成JAR归档文件,我们必须到我们操作系统的shell里去编译它。

javac -d . AppExample.java

Then we can pack it into a JAR:

然后我们可以把它打包成一个JAR。

jar cvf example.jar com/baeldung/manifest/AppExample.class

Our example.jar will contain a default manifest file. We can now try to execute the JAR:

我们的example.jar将包含一个默认的清单文件。现在我们可以尝试执行JAR。

java -jar example.jar

Execution will fail with an error:

执行将失败,出现错误。

no main manifest attribute, in example.jar

3. Executing a JAR With the Main Manifest Attribute

3.执行带有主manifest属性的JAR

As we have seen, JVM couldn’t find our main manifest attribute. Because of that, it couldn’t find our main class containing our main method.

正如我们所见,JVM无法找到我们的main manifest属性。正因为如此,它无法找到包含主方法的主类。

Let’s include a proper manifest attribute into the JAR along with our code. We’ll need to create a MANIFEST.MF file containing a single line:

让我们把适当的清单属性和我们的代码一起纳入JAR。我们需要创建一个MANIFEST.MF文件,其中包含一行。

Main-Class: com.baeldung.manifest.AppExample

Our manifest now contains the classpath to our compiled AppExample.class.
Since we already compiled our example class, there’s no need to do it again.

我们的清单现在包含了我们编译的AppExample.class的classpath
由于我们已经编译了我们的示例类,所以没有必要再做一次。

We’ll just pack it together with our manifest file:

我们就把它和我们的清单文件打包在一起。

jar cvmf MANIFEST.MF example.jar com/baeldung/manifest/AppExample.class

This time JAR executes as expected and outputs:

这次JAR如期执行并输出。

AppExample executed!

4. Conclusion

4.结论

In this quick article, we showed how to pack a simple Java class as a self-executing JAR, and we demonstrated the importance of a main manifest attribute on two simple examples.

在这篇快速文章中,我们展示了如何将一个简单的Java类打包成一个自执行的JAR,我们还在两个简单的例子上证明了主清单属性的重要性。

The complete source code for the example is available over on GitHub. This is a Maven-based project, so it can be imported and used as-is.

该示例的完整源代码可在GitHub上找到。这是一个基于Maven的项目,因此可以按原样导入和使用。