JAR, WAR, EAR
2 min readFeb 22, 2021
Different ways to package java files for deployment.
EAR (Application Server) > WAR (Web Server) > JAR (JRE)
Notes based on these articles
Java ARchive (JAR)
Application JAR can be executed in any JRE env without Server.
- compiled java classes .class
- resources (image, etc)
- other JARs (dependency libraries)
- META-INF/MANIFEST.MF etc
- Application JAR (has starting point main()) vs Library JAR
Web ARchive (WAR)
Deployed in Web Server/Web Container in Application Server
- JAR
- JSP, Servlets, HTML, CSS and any images needed to render the Web page
- WEB-INF/web.xml etc
Enterprise Application ARchive (EAR)
Deployed in Java EE application server for large enterprise applications.
- META-INF/application.xml
- WAR
- JAR
Create a JAR
$ vi Hello.java // Put main method here$ javac Hello.java // Create .class$ vi Hello.mf // Create manifest specifying entry point
$ cat Hello.mf
Main-Class: Hello$ jar -cvmf Hello.mf Hello.jar Hello.class // Create jar$ java -jar Hello.jar // Execute
Hello!$ java -cp Hello.jar Hello // Execute w/o mf - specify entry point
Hello!
Reference: https://docs.oracle.com/javase/tutorial/deployment/jar/index.html
Creating a WAR
$ java -cvf HelloWeb.war * // Top directory containing WEB-INF dir
How to deploy WAR on Docker. Notes from:
# Create Dockerfile
FROM tomcat:8.0MAINTAINER your_name# COPY path-to-your-application-war path-to-webapps-in-docker-tomcat
COPY some-app/target/some-app.war /usr/local/tomcat/webapps
Build Docker image and run in a container
docker image build -t your_name/some-app-imagedocker run -it --publish 8081:8080 your_name/some-app-image
Create an EAR
$ java -cvf myApplication.ear Hello.jar
$ java -cvf myApplication.ear HelloWeb.war