jar and war

  • JAR: Java ARchive
  • WAR: Web application ARchive


From Java Tips: Difference between ear jar and war files:

These files are simply zipped files using java jar tool. These files are created for different purpose. Here is the description of those files:

  • .jar files: The .jar files contain the libraries, resources and accessories files like property files.
  • .war files: The war file contains the web application that can be deployed on the any servlet/jsp container. The .war file contains jsp, html, javascript and other files for necessary for the development of web applications.
  • .ear files: The .ear file contains the EJB modules of the applications.

Official Sun/Oracle description:

Reference:


簡單來說:基本上都是壓縮檔案。war用於具有特定目錄結構的Web應用程序。


convert to zip

In summary, EAR, JAR, and WAR files are all simply zip files that contain the various images, XML files, property files and pieces of Java code that make up a Java application. If the .ear, .war or .jar extension or any of these files is changed to .zip, it can be opened with any standard decompression tool.

Tip: Some files have unique folder structure and requirements (e.g. some stuff needs to be in a WEB-INF directory).

naming war file

For popular servlet containers (JBoss, Tomcat, Jetty), WAR naming convention can drive context paths. Name of the war becomes the context path if no explicit context path is defined anywhere.

a.war > localhost:8080/a
b.war > localhost:8080/b


  • JBoss

For web applications that are deployed outside and EAR file, the context root can be specified in two ways. First, the context root can be specified in the WEB-INF/jboss-web.xml file. The following example shows what the jboss- web.xml file would look like if it weren’t bundled inside of an EAR file.

<jboss-web>
  <context-root>bank</context-root>

Finally, if no context root specification exists, the context root will be the base name of the WAR file. For web-client.war, the context root would default to web-client. The only special case to this naming special name ROOT.


  • Tomcat

When autoDeploy or deployOnStartup operations are performed by a Host, the name and context path of the web application are derived from the name(s) of the file(s) that define(s) the web application. Consequently, the context path may not be defined in a META-INF/context.xml embedded in the application and there is a close relation between the context name, context path, context version and the base file name (the name minus any .war or .xml extension) of the file.


  • Jetty
  • If the WAR file is named myapp.war, then the context will be deployed with a context path of /myapp
  • If the WAR file is named ROOT.WAR (or any case insensitive variation), then the context will be deployed with a context path of /
  • If the WAR file is named ROOT-foobar.war (or any case insensitive variation), then the context will be deployed with a context path of / and a virtual host of “foobar”

Reference: Chapter 5. Configuring Contexts

webapp directory



  • webapp:工程發佈文件夾,每個war包都可以視為webapp的壓縮包
  • META-INF
    • 用於存放工程自身相關的一些信息、元文件信息,通常由開發工具環境自動生成(配置清单文件)
    • 若要執行jar檔的元數據文件,此為必須。如果將jar中的META-INF文件夾刪除,那麼jar文件裡邊就沒有MANIFEST.MF文件,java-jar就找不到main class
  • WEB-INF
    • Java web應用的安全目錄,客戶端無法訪問,只有服務端可以訪問的目錄
    • 此目錄包含一個層次結構,是webapp的必要配置信息,沒有它無法運行webapp,以及jsp、servlet、和類的所有文件(e.g. classes, libs)
  • /WEB/classes:存放程序所需要的所有Java class文件
  • /WEB/lib:存放程序所需要的所有jar文件
  • /WEB/web.xml:web應用的部署配置文件。它是工程中最重要的配置文件,它描述了servlet和組成應用的其他組件,以及應用初始化參數、安全管理約束等


p.s. META-INF文件的相關資料:META-INF文件夹是干啥的,META-INF文件夹的作用, META-INF文件夹能删吗 - 逃离沙漠 - 博客园