Sobes.tech
Senior

How does JAR differ from WAR?

sobes.tech AI

Answer from AI

JAR (Java ARchive) — is an archive file that contains compiled Java classes and resources used to run Java applications or libraries. It is designed for packaging and distributing Java code.

WAR (Web Application Archive) — is a specialized archive intended for Java web applications. It contains a directory structure with servlets, JSPs, configuration files (e.g., web.xml), static resources (HTML, CSS, JS), and other web application components.

Main differences:

  • Purpose: JAR — for general Java code and libraries; WAR — for web applications.
  • Structure: WAR has a specific directory structure (WEB-INF, META-INF), JAR — more flexible.
  • Execution: WAR is deployed on an application server (e.g., Tomcat), JAR can be run directly (if it contains a main class).

Example of WAR structure:

myapp.war/
  WEB-INF/
    web.xml
    classes/
    lib/
  index.html
  styles.css

Example of running a JAR:

java -jar myapp.jar
How does JAR differ from WAR? — Embedded / IoT - sobes.tech