Showing posts with label war. Show all posts
What Is a WAR File?
A WAR is a ZIP file with a specific directory structure. It contains:
WEB-INF/web.xml— the deployment descriptor (servlet mappings, listeners, filters)WEB-INF/classes/— compiled Java.classfilesWEB-INF/lib/— all dependency JARs bundled into the appMETA-INF/MANIFEST.MF— archive metadata- Static resources (HTML, CSS, JS, images) in the root or subdirectories
The WEB-INF/ directory is special: files inside it are never served directly to clients by the servlet container.
Method 1: List Contents Without Extracting
jar -tvf myapp.war
Flags: -t (table/list), -v (verbose), -f (filename). Pipe through grep to find specific files:
jar -tvf myapp.war | grep "web.xml"
jar -tvf myapp.war | grep "\.properties"
jar -tvf myapp.war | grep "spring"
Example output:
0 Thu Nov 30 12:00:00 EST 2023 META-INF/
106 Thu Nov 30 12:00:00 EST 2023 META-INF/MANIFEST.MF
0 Thu Nov 30 12:00:00 EST 2023 WEB-INF/
742 Thu Nov 30 12:00:00 EST 2023 WEB-INF/web.xml
4096 Thu Nov 30 12:00:00 EST 2023 WEB-INF/classes/com/example/MyServlet.class
12345 Thu Nov 30 12:00:00 EST 2023 WEB-INF/lib/spring-core-5.3.20.jar
Method 2: Extract Everything
jar -xvf myapp.war
To extract into a specific folder:
mkdir -p extracted/myapp
cd extracted/myapp
jar -xvf ../../myapp.war
Method 3: Extract a Single File
# Extract just web.xml
jar -xvf myapp.war WEB-INF/web.xml
# Extract a specific class file
jar -xvf myapp.war WEB-INF/classes/com/example/MyServlet.class
# Extract a properties file
jar -xvf myapp.war WEB-INF/classes/application.properties
Method 4: Use unzip
Since WAR files are ZIP archives, unzip works and is often faster for large files:
# List contents (no extraction)
unzip -l myapp.war
# Extract everything
unzip myapp.war -d extracted/
# Extract a single file
unzip myapp.war WEB-INF/web.xml
# Extract all .properties files
unzip myapp.war "WEB-INF/classes/*.properties" -d config/
Typical WAR File Structure
myapp.war
├── META-INF/
│ └── MANIFEST.MF
├── WEB-INF/
│ ├── web.xml ← deployment descriptor
│ ├── classes/ ← compiled .class files
│ │ ├── com/example/MyServlet.class
│ │ └── application.properties
│ └── lib/ ← dependency JARs
│ ├── spring-core.jar
│ └── hibernate-core.jar
├── index.html ← publicly accessible
├── css/
└── js/
| Location | Browser-accessible? | Purpose |
|---|---|---|
Root (/) | Yes | Static files (HTML, CSS, JS, images) |
WEB-INF/ | No | Private — config, compiled code, libs |
WEB-INF/classes/ | No | Compiled Java classes and resources |
WEB-INF/lib/ | No | Bundled dependency JARs |
META-INF/ | No | Archive metadata |
How to Build a WAR File
With Maven — add <packaging>war</packaging> to your pom.xml, then:
mvn clean package
# WAR created at target/myapp.war
With Gradle — apply the war plugin in build.gradle, then:
./gradlew war
# WAR created at build/libs/myapp.war
Viewing Class File Contents
To inspect a .class file's bytecode (after extracting it):
# Show method signatures
javap WEB-INF/classes/com/example/MyServlet.class
# Show full bytecode disassembly
javap -c WEB-INF/classes/com/example/MyServlet.class
For human-readable source code, use a Java decompiler like CFR.
Platform Support
| Platform | Notes |
|---|---|
| Linux / macOS | jar is in $JAVA_HOME/bin, usually in PATH |
| Windows | Same command in CMD or PowerShell if JDK is in PATH |
| No JDK | Use unzip, 7-Zip, WinRAR, or Mac's Archive Utility |
Quick Reference
jar -tvf myapp.war # list contents
jar -tvf myapp.war | grep web.xml # find a specific file
jar -xvf myapp.war # extract everything
jar -xvf myapp.war WEB-INF/web.xml # extract one file
unzip -l myapp.war # list with unzip
unzip myapp.war -d output/ # extract to a folder