Difference Between WAR and EAR Files – WAR和EAR文件的区别

最后修改: 2022年 7月 8日

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

1. Overview

1.概述

In this tutorial, we’ll look at the differences between the Java JAR (Java ARchive), WAR (Web Application ARchive) and EAR (Enterprise Application aRchive) artifact files.

在本教程中,我们将看看Java JAR(Java ARchive)、WAR(Web Application ARchive)和EAR(Enterprise Application aRchive)工件文件的区别。

We’ll discuss what each file format accomplishes and how to use it.

我们将讨论每种文件格式的作用以及如何使用它。

2. Java Artifact File Formats

2 Java工件文件格式

The Java specification contains multiple types of artifact files. The most prominent are JAR, WAR, and EAR, but there are others as well. Starting with the JAR, each type builds upon the other, respectively.

Java规范包含多种类型的工件文件。最突出的是JAR、WAR和EAR,但也有其他的。从JAR开始,每种类型都分别建立在其他的基础上。

Although the JAR file format is part of the JavaSE specification, WAR and EAR files are themselves part of JavaEE, now known as JakartaEE.

尽管JAR文件格式是JavaSE规范的一部分,但WAR和EAR文件本身就是JavaEE的一部分,现在被称为JakartaEE。

3. JAR Files

3.JAR文件

As we know, the JAR file is the basic artifact for Java applications.

正如我们所知,JAR文件是Java应用程序的基本工件

Under the hood, it’s essentially a .zip file with the .jar extension. It includes contents such as classes, resources, and meta information files.

在引擎盖下,它本质上是一个.zip文件,扩展名为.jar。它包括类、资源和元信息文件等内容。

We can create JAR files with the jar program and run them using the java -jar command on any system with just the Java Runtime Environment (JRE) .

我们可以用jar程序创建JAR文件,并使用java -jar命令在任何只有Java运行环境(JRE)的系统上运行jar

4. WAR Files

4.WAR文件

WAR files are used for bundling Java web applications.

WAR文件用于捆绑Java网络应用

They’re also .zip files under the hood and are an extension of the JAR file format.

它们也是.zip文件,是JAR文件格式的一个扩展。

Along with classes and resources, WAR files can contain other JARs in the form of libraries, JSP files, Java Servlets, as well as all types of static web files (HTML, CSS, Javascript, etc.).

除了类和资源之外,WAR文件还可以包含其他JAR,其形式包括库、JSP文件Java Servlets,以及所有类型的静态Web文件(HTML、CSS、Javascript等)。

JakartaEE specifies that correctly-formatted WAR files must contain a WEB-INF directory, which itself contains a web.xml file. This is where we declare the structure and configurations of the web application, such as the web paths of each servlet or the session timeout amount.

JakartaEE规定,格式正确的WAR文件必须包含一个WEB-INF目录,它本身包含一个web.xml文件。这就是我们声明Web应用程序的结构和配置的地方,例如每个Servlet的Web路径或会话超时量。

In contrast to JARs, WAR files cannot be run as standalone applications and we can only use them as components of another application, like a servlet container or an application server.

与JAR相比,WAR文件不能作为独立的应用程序运行,我们只能将其作为另一个应用程序的组成部分,如Servlet容器或应用服务器

The process of putting a WAR file into a server is known as application deployment. Typical Application Servers include Tomcat, Jetty and Wildfly.

将WAR文件放入服务器的过程被称为应用部署。典型的应用服务器包括Tomcat、Jetty和Wildfly。

5. EAR Files

5.EAR文件

EAR files are used for enterprise applications composed of multiple modules.

EAR文件用于由多个模块组成的企业应用程序。

There are multiple types of modules, but the most common are web modules, essentially WAR files, and EJB modules which are special types of JAR files that contain Enterprise Java Bean classes.

有多种类型的模块,但最常见的是Web模块,基本上是WAR文件,以及EJB模块,这是包含企业Java Bean类的特殊类型的JAR文件。

Similar to WAR, the EAR is a JAR extension and must contain a special XML file, named application.xml, under a root META-INF directory. In this file, we describe the enterprise application and list its modules. Additionally, we can add security roles for the whole app.

与WAR类似,EAR是一个JAR扩展,必须包含一个特殊的XML文件,名为application.xml,在根META-INF目录下。在这个文件中,我们描述了企业应用程序并列出了它的模块。此外,我们可以为整个应用程序添加安全角色。

Like WAR, an EAR file also cannot be run as a standalone application. We must deploy it on an application server.

与WAR一样,EAR文件也不能作为一个独立的应用程序运行。我们必须将其部署在应用服务器上。

However, in this case, not all types are compatible. We must use a server that supports JakartaEE technologies such as Wildfly, but we can’t use Jetty. As for Tomcat, it’s also incompatible, but there’s a different flavor, named TomEE, which is compatible.

然而,在这种情况下,不是所有的类型都能兼容。我们必须使用支持JakartaEE技术的服务器,如Wildfly,但我们不能使用Jetty。至于Tomcat,它也是不兼容的,但有一种不同的味道,名为TomEE,它是兼容的。

The advantage of the EAR format is that it allows us to specify a complex multi-component application in a single file. Additionally, we can avoid duplication by sharing common resources. Finally, the single file format is very helpful because it eliminates deployment errors.

EAR格式的优点是,它允许我们在一个文件中指定一个复杂的多组件应用程序。此外,我们可以通过共享公共资源来避免重复。最后,单一文件格式非常有帮助,因为它消除了部署错误。

6. Conclusion

6.结语

In this article, we looked at the differences between WAR and EAR files. We learned the structure of each file and discussed their typical use cases. Finally, we saw how to use each type of artifact.

在这篇文章中,我们研究了WAR和EAR文件的区别。我们了解了每个文件的结构并讨论了它们的典型使用情况。最后,我们看到如何使用每种类型的工件。