Jar vs War in Spring Boot: Key Differences and Usage Guide
Jar is an executable package that contains an embedded server, allowing you to run the app standalone. A War is a traditional web archive meant to be deployed on an external servlet container like Tomcat. Use Jar for simplicity and standalone apps, and War when integrating with existing servlet servers.Quick Comparison
Here is a quick side-by-side comparison of Jar and War packaging in Spring Boot.
| Factor | Jar | War |
|---|---|---|
| Packaging Type | Executable Jar with embedded server | Web Archive for external server |
| Server Requirement | No external server needed | Requires external servlet container |
| Deployment | Run as standalone app (java -jar) | Deploy to servlet container (e.g., Tomcat) |
| Configuration | Simpler, embedded server auto-configured | Requires server setup and deployment descriptors |
| Use Case | Microservices, standalone apps | Legacy apps, shared servlet environments |
| Build Complexity | Simpler build process | More complex, needs war packaging setup |
Key Differences
Jar packaging in Spring Boot bundles your application and an embedded servlet container (like Tomcat or Jetty) into a single executable file. This means you can run your app directly with java -jar without needing to install or configure a separate server. It simplifies deployment and is ideal for microservices or cloud-native apps.
On the other hand, War packaging creates a traditional web archive that must be deployed to an external servlet container. This approach is useful when you need to integrate with existing servlet servers or share the server environment with other applications. It requires more setup, including deployment descriptors and server configuration.
In Spring Boot, switching between Jar and War mainly involves changing the packaging type in your build file and adjusting your main application class to extend SpringBootServletInitializer for War deployment.
Jar Code Example
This is a simple Spring Boot application packaged as a Jar. It runs standalone with an embedded server.
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @GetMapping("/") public String home() { return "Hello from Jar!"; } }
War Equivalent
This is the equivalent Spring Boot application packaged as a War. It requires deployment to an external servlet container.
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class DemoApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @GetMapping("/") public String home() { return "Hello from War!"; } }
When to Use Which
Choose Jar packaging when you want a simple, standalone Spring Boot application that runs with an embedded server. This is best for microservices, cloud deployments, or when you want to avoid managing external servers.
Choose War packaging when you need to deploy your Spring Boot app to an existing servlet container or application server, such as in legacy environments or shared hosting. It allows integration with other web apps on the same server.