Java Application Packaging with JAR, WAR, EAR

Loading

Packaging Java applications is a crucial step in the deployment process. Java provides several packaging formats, each suited for different types of applications. Below is a comprehensive guide to packaging Java applications using JAR, WAR, and EAR files.


1. JAR (Java Archive)

  • Purpose: Used for packaging Java classes and associated metadata and resources into a single file.
  • Use Case: Standalone Java applications or libraries.

Creating a JAR File

  1. Compile Java Classes:
   javac -d target/classes src/main/java/com/example/*.java
  1. Create the JAR File:
   jar cf myapp.jar -C target/classes .
  1. Run the JAR File:
   java -jar myapp.jar

Manifest File

  • Purpose: Specifies metadata like the main class.
  • Example:
  Manifest-Version: 1.0
  Main-Class: com.example.Main
  • Include Manifest in JAR:
  jar cfm myapp.jar MANIFEST.MF -C target/classes .

2. WAR (Web Application Archive)

  • Purpose: Used for packaging web applications, including servlets, JSPs, and static resources.
  • Use Case: Web applications deployed on servlet containers like Apache Tomcat.

Creating a WAR File

  1. Directory Structure:
   mywebapp/
   ├── WEB-INF/
   │   ├── classes/
   │   ├── lib/
   │   └── web.xml
   ├── index.jsp
   └── static/
       └── styles.css
  1. Compile Java Classes:
   javac -d WEB-INF/classes src/main/java/com/example/*.java
  1. Create the WAR File:
   jar cf mywebapp.war -C mywebapp .
  1. Deploy the WAR File:
  • Copy the WAR file to the webapps directory of your servlet container.
  • Restart the server if necessary.

web.xml

  • Purpose: Deployment descriptor for configuring servlets, filters, and other web components.
  • Example:
  <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
           http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
           version="3.1">
      <servlet>
          <servlet-name>HelloServlet</servlet-name>
          <servlet-class>com.example.HelloServlet</servlet-class>
      </servlet>
      <servlet-mapping>
          <servlet-name>HelloServlet</servlet-name>
          <url-pattern>/hello</url-pattern>
      </servlet-mapping>
  </web-app>

3. EAR (Enterprise Archive)

  • Purpose: Used for packaging enterprise applications, including EJBs, web modules, and other resources.
  • Use Case: Enterprise applications deployed on Java EE application servers like WildFly or WebLogic.

Creating an EAR File

  1. Directory Structure:
   myenterpriseapp/
   ├── META-INF/
   │   └── application.xml
   ├── mywebapp.war
   └── myejb.jar
  1. Create the EAR File:
   jar cf myenterpriseapp.ear -C myenterpriseapp .
  1. Deploy the EAR File:
  • Use the administration console or CLI of your application server to deploy the EAR file.

application.xml

  • Purpose: Deployment descriptor for configuring enterprise application modules.
  • Example:
  <application xmlns="http://xmlns.jcp.org/xml/ns/javaee"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
               http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd"
               version="7">
      <module>
          <web>
              <web-uri>mywebapp.war</web-uri>
              <context-root>/mywebapp</context-root>
          </web>
      </module>
      <module>
          <ejb>myejb.jar</ejb>
      </module>
  </application>

Best Practices

  1. Use Build Tools: Use Maven or Gradle for automated packaging.
  2. Minimize Dependencies: Only include necessary dependencies to reduce package size.
  3. Versioning: Use version numbers in file names for easier management.
  4. Testing: Test the packaged application in a staging environment before deployment.

Resources


Leave a Reply

Your email address will not be published. Required fields are marked *