Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

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.

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

2. Java Artifact File Formats

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.

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

3. JAR Files

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

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

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

4. WAR Files

WAR files are used for bundling Java web applications.

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

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

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.

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.

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

5. EAR Files

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

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.

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.

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

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.

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.

6. Conclusion

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.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!