Backend Development 13 min read

Understanding the Difference Between JAR and WAR Packages in Spring Boot and How to Build Them

This article explains why a Spring Boot application behaves differently when run as a JAR versus deployed as a WAR, outlines the historical context of servlet containers, compares JAR and WAR formats, and provides step‑by‑step Maven commands and pom.xml modifications for packaging both types.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding the Difference Between JAR and WAR Packages in Spring Boot and How to Build Them

The article begins with a puzzling scenario: a Spring Boot project packaged as a JAR runs on a machine using java -jar with the configured server.port , but when the same project is repackaged as a WAR and deployed to an external Tomcat, the port reverts to Tomcat's default 8080 and the context path includes the project name.

It explains that a JAR launch starts Spring Boot's embedded Tomcat, so the application's own configuration (including server.port ) is used, whereas a WAR deployment disables the embedded server and relies entirely on the external Tomcat's settings.

The article then gives a brief historical overview of Java EE, servlet standards, and the evolution from heavyweight containers like Tomcat and JBoss to lightweight embedded servers such as Jetty, highlighting the modern "use JAR, not WAR" micro‑service philosophy.

Key differences between JAR and WAR are listed: a WAR is a web module containing a WEB-INF directory and is meant for deployment to a servlet container; a JAR is a generic archive that includes compiled classes and a Main-Class entry, allowing direct execution with java -jar . The article also notes JAR's additional features like digital signing, compression, and platform extensions.

Packaging as JAR : 1) Create a Spring Starter Project (default packaging is JAR). 2) Example pom.xml snippet is shown. 3) Build with mvn clean package . The resulting JAR appears in the target directory.

<project xmlns="http://maven.apache.org/POM/4.0.0" ...> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

Packaging as WAR : 1) Add a ServletInitializer class in the same package as the main application. 2) Change the packaging element in pom.xml from jar to war . 3) Exclude the embedded Tomcat starter and add dependencies for javax.servlet-api (provided) and tomcat-servlet-api (provided). 4) Build with mvn clean package to produce a WAR file, then copy it to Tomcat's webapps directory and start Tomcat.

<project ...> ... <packaging>war</packaging> ... <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-servlet-api</artifactId> <version>8.0.36</version> <scope>provided</scope> </dependency> ... </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

The article notes that selecting war as the packaging type when creating the project automatically generates the ServletInitializer class, and the resulting pom looks similar to the one shown above.

Finally, the author encourages readers to keep the guide for future reference and provides a friendly sign‑off.

mavenSpring BootpackagingTomcatJarWAR
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.