Differences and Similarities Between JAR, WAR, and EAR

In J2EE applications, modules are packaged as JAR, WAR, or EAR depending on their function.

Jar,War,Ear

JAR

  • Java ARchive
  • An archive that contains the class files and configuration files required to run a Java program.
  • It is used to package related files into a single file when distributing an application written in Java. In practice, many class libraries are distributed in this format.
  • A compressed file that contains class files, related resource files used by those classes, and metadata.
  • It is actually compressed in ZIP file format.
  • It can be run if a JRE (Java Runtime Environment) is available.
    • java -jar {project_name}.jar

WAR

  • Web application ARchive
  • An archive that contains class files (servlets), configuration files (XML), JSP or HTML files, and JAR-format libraries used by a Java web application.
  • It includes web.xml, the definition file for web application configuration.
  • When a WAR file that includes web.xml is deployed to an application server such as Tomcat, deployment is based on that file.
  • It uses a predefined structure.
    • WEB-INF, META-INF

EAR

  • Enterprise ARchive
  • A package format extension for applications based on Java EE (Java Platform, Enterprise Edition).
  • It can contain any number of WAR files, EJB files, and the JAR files required by those applications.
  • All of the files above (.jar, .war) are packaged into a JAR file with the .ear (enterprise archive) extension and deployed to an application server.
  • The application.xml file contained in the META-INF folder is required.

Differences Between JAR, WAR, and EAR

  • The biggest difference between JAR, WAR, and EAR files is that they target different environments.
  • Their file sizes are generally ordered as EAR > WAR > JAR.

Similarities Between JAR, WAR, and EAR

  • All are ZIP-format packages (archives that organize multiple files) defined according to Java community specifications.
  • They are compressed files created with Java Jar (java -jar) and package related files (resources, property files, and so on) so applications can be deployed and run easily.

Summary

In short, JAR and WAR are the same kind of compressed file, differing only in the range of files compressed according to their purpose. Recently, because of frameworks such as Spring Boot, executable JAR files are often used even for web applications.