JAR vs WAR Files
Fast recall
Quick revision
Core answer
Whether the learner can separate archive layout, executable packaging, and the servlet-container deployment model.
Saying every JAR contains an embedded server or every WAR requires `web.xml`.
Explain that Spring Boot can repackage both formats and that the server exists only for a web application that includes one.
Interview-ready guide
Interview answer
A JAR is the general Java archive format. It can contain a library or application classes and resources, and it is directly launchable only when it has the required entry information and runtime contents. A WAR is a web-application archive with the servlet layout, including WEB-INF/classes and WEB-INF/lib, intended for a compatible servlet container. Modern WARs do not always need web.xml.
For example, a Spring Boot REST service is commonly repackaged as an executable JAR with its runtime dependencies and a servlet server dependency, then run with java -jar. A company that deploys applications into an existing external servlet container may package the same kind of app as a WAR and mark the external container dependency appropriately. Spring Boot can also make an executable WAR, so the important distinction is archive layout and deployment model rather than 'JAR always runs itself, WAR never does.'
JAR vs WAR Files
Direct Answer
A JAR is a general Java archive for classes and resources; it can be a library or an executable application. A WAR follows the web-application archive layout for deployment to a servlet container. Spring Boot can build executable JARs or WARs with nested runtime dependencies, but an embedded servlet server appears only in web applications that include one.
Code Example
# JAR (Spring Boot, embedded Tomcat) — runs itself
mvn package
java -jar target/order-service-1.0.0.jar
# WAR (deploy to an external servlet container)
# pom.xml: <packaging>war</packaging>
mvn package # produces target/order-service-1.0.0.war
cp target/order-service-1.0.0.war $TOMCAT_HOME/webapps/ # external Tomcat hosts it
Common Mistakes & Pitfalls
- Saying every JAR is executable: a library JAR usually has no application entry point.
- Saying every executable JAR embeds Tomcat: non-web apps have no web server, and web apps can use another supported server.
- Treating
web.xmlas mandatory: modern servlet applications may use annotations or programmatic configuration. - Saying a WAR can never run directly: Spring Boot can create an executable WAR while retaining WAR layout.
- Deploying a Boot JAR as a WAR: select the packaging and dependency setup for the actual target container.
Check your understanding
Answer these without looking back at the article.
- How does Spring Boot's embedded Tomcat work?
- When would you still use WAR packaging?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.