Differences Between JAR and WAR Packaging – JAR和WAR打包之间的区别

最后修改: 2019年 3月 31日

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

1. Overview

1.概述

In this quick tutorial, we’ll focus on the differences between JAR and WAR packaging in Java.

在这个快速教程中,我们将重点介绍Java中JAR和WAR打包的区别。

First, we’ll define each packaging option separately. Afterwards, we’ll summarize their differences.

首先,我们将分别定义每个包装选项。之后,我们将总结它们的区别。

2. JAR Packaging

2.JAR打包

Simply put, JAR – or Java Archive – is a package file format. JAR files have the .jar extension and may contain libraries, resources, and metadata files.

简单地说,JAR–或Java Archive–是一种包文件格式。JAR文件的扩展名是.jar,可能包含库、资源和元数据文件

Essentially, it’s a zipped file containing the compressed versions of .class files and resources of compiled Java libraries and applications.

从本质上讲,它是一个压缩文件,包含.class文件的压缩版本和已编译的Java库和应用程序的资源。

For example, here’s a simple JAR file structure:

例如,这里有一个简单的JAR文件结构。

META-INF/
    MANIFEST.MF
com/
    baeldung/
        MyApplication.class

The META-INF/MANIFEST.MF file may contain additional metadata about the files stored in the archive.

META-INF/MANIFEST.MF文件可能包含有关存储在存档中的文件的额外元数据。

We can create a JAR file using the jar command or with tools like Maven.

我们可以使用jar命令或使用Maven等工具创建一个JAR文件。

3. WAR Packaging

3.WAR包装

WAR stands for Web Application Archive or Web Application Resource. These archive files have the .war extension and are used to package web applications that we can deploy on any Servlet/JSP container.

WAR是Web应用档案或Web应用资源的缩写。这些归档文件有.war扩展名,用于打包Web应用程序,我们可以在任何Servlet/JSP容器上部署。

Here’s an example layout of a typical WAR file structure:

下面是一个典型的WAR文件结构布局的例子。

META-INF/
    MANIFEST.MF
WEB-INF/
    web.xml
    jsp/
        helloWorld.jsp
    classes/
        static/
        templates/
        application.properties
    lib/
        // *.jar files as libs

Inside, it has a META-INF directory holding useful information in the MANIFEST.MF about the web archive. The META-INF directory is private and can’t be accessed from the outside.

在内部,它有一个META-INF目录,在MANIFEST.MF中保存着关于网络档案的有用信息。META-INF目录是私有的,不能从外部访问。

On the other hand, it also contains the WEB-INF public directory with all the static web resources, including HTML pages, images, and JS files. Moreover, it contains the web.xml file, servlet classes, and libraries.

另一方面,它还包含WEB-INF公共目录,其中有所有的静态网络资源,包括HTML页面、图像和JS文件。此外,它还包含web.xml文件、Servlet类和库。

We can use the same tools and commands that we used to build a JAR to build a .war archive.

我们可以使用与构建JAR相同的工具和命令来构建.war存档。

4. Key Differences

4.关键差异

So, what are the key differences between these two archive types?

那么,这两种档案类型之间的关键区别是什么?

The first and most obvious difference is the file extension. JARs have the .jar extension, whereas the WAR file has the .war extension.

第一个也是最明显的区别是文件扩展名。JAR的扩展名为.jar,而WAR文件的扩展名为.war

The second main difference is their purpose and the way they function. JAR files allow us to package multiple files in order to use it as a library, plugin, or any kind of application. On the other hand, WAR files are used only for web applications.

第二个主要区别是它们的目的和功能方式。JAR文件允许我们打包多个文件,以便将其作为一个库、插件或任何类型的应用程序使用。另一方面,WAR文件只用于网络应用。

The structure of the archives is also different. We can create a JAR with any desired structure. In contrast, WAR has a predefined structure with WEB-INF and META-INF directories.

档案的结构也是不同的。我们可以用任何想要的结构来创建一个JAR。相反,WAR有一个预定义的结构,有WEB-INFMETA-INF目录。

Finally, we can run a JAR from the command line if we build it as an executable JAR without using additional software. Or, we can use it as a library. In contrast, we need a server to execute a WAR.

最后,如果我们将JAR构建为可执行JAR,我们可以从命令行运行JAR,而无需使用其他软件。或者,我们可以将其作为一个库使用。相比之下,我们需要一个服务器来执行WAR

5. Conclusion

5.结论

In this quick article, we compared the .jar and .war Java packaging types. In doing so, we also noted that while both use the same ZIP file format, there are several important differences.

在这篇快速文章中,我们比较了.jar.war Java打包类型。在此过程中,我们还注意到,虽然两者使用相同的ZIP文件格式,但有几个重要区别。