Differences Between JAR and WAR Packages and How to Build Spring Boot Projects as JAR or WAR
This article explains the differences between JAR and WAR files, why Spring Boot runs with an embedded Tomcat when packaged as a JAR, how deployment changes when using an external Tomcat, and provides step‑by‑step instructions with Maven configurations to build Spring Boot applications as either JAR or WAR packages.
The author, a senior architect, presents a common issue where a Spring Boot application packaged as a JAR runs with an embedded Tomcat, while the same project packaged as a WAR deployed to an external Tomcat uses the container’s default port and context path.
When the JAR is executed with java -jar app.jar , the embedded Tomcat reads the server.port configuration from the application’s properties. Deploying the WAR to a standalone Tomcat disables the embedded server, so the container’s settings (port 8080, context path) take effect.
The article then reviews the historical evolution from early CGI scripts to the Java EE servlet standard, the emergence of Tomcat as a servlet container, and the later trend of lightweight embedded servers such as Jetty and Undertow that enable the “use JAR, not WAR” approach.
Differences between JAR and WAR
JAR files are general‑purpose archives that contain compiled classes and a Main-Class entry, allowing them to be launched directly with the java command. WAR files are specialized web archives that must contain a WEB-INF directory, servlet classes, and optional libraries, and they are intended to be deployed to a servlet container.
The article lists several characteristics of the JAR format, such as digital signing for security, compression to reduce download size, and the ability to bundle extensions for the Java platform.
Packaging a Spring Boot project as a JAR
Create a new Spring Starter Project in IDE; the default packaging is jar .
Use the generated pom.xml (example shown) with the Spring Boot parent and necessary dependencies.
Run mvn clean package in the terminal; the resulting .jar appears in the target directory.
Packaging the same project as a WAR
Add a ServletInitializer class in the same package as the main application class.
Change the packaging element in pom.xml from jar to war .
Exclude the embedded Tomcat starter and add javax.servlet-api and tomcat-servlet-api dependencies with provided scope.
Run mvn clean package again; a .war file is generated and can be dropped into Tomcat’s webapps directory.
Additional notes mention that selecting “war” as the packaging type when creating the project automatically generates the ServletInitializer , and that the same Maven commands work for both formats.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.