Skip to content

Instantly share code, notes, and snippets.

@deomorxsy
Last active April 7, 2024 19:46
Show Gist options
  • Save deomorxsy/68a68180461830638d45c3dc666dd195 to your computer and use it in GitHub Desktop.
Save deomorxsy/68a68180461830638d45c3dc666dd195 to your computer and use it in GitHub Desktop.
handling jar compressed files

Handling jar compressed files

Quick notes about handling jars on the CLI in maven projects with java. Dependencies:

  • jar
  • unzip
  • mvn

generate jar passing the directory the pom.xml is located:

; mvn package -f ./server/

path to the jar:

; file ./server/target/server-1.0-SNAPSHOT.jar
./server/target/server-1.0-SNAPSHOT.jar: Java archive data (JAR)

list contents with index of the jar

; jar tf ./server/target/server-1.0-SNAPSHOT.jar

to find the mainClass of the generated jar, you can decompress the META-INF/MANIFEST.MF file to stdout:

; unzip -p ./server/target/server-1.0-SNAPSHOT.jar META-INF/MANIFEST.MF
Manifest-Version: 1.0
Created-By: Maven JAR Plugin 3.3.0
Build-Jdk-Spec: 17
Implementation-Title: server
Implementation-Version: 1.0-SNAPSHOT
Main-Class: org.springframework.boot.loader.launch.JarLauncher
Start-Class: com.organization.app.App
Spring-Boot-Version: 3.2.4
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Spring-Boot-Layers-Index: BOOT-INF/layers.idx

;

Above, the mainClass is "Main-Class: org.springframework.boot.loader.launch.JarLauncher". Now, you can execute the app in these two ways:

  1. directly execute the mainClass app in the jar:
; java -cp ./server/target/server-1.0-SNAPSHOT.jar org.springframework.boot.loader.launch.JarLauncher
Hello World!
;
  1. ...or just pass the jar file as parameter using the jar flag:
; java -jar ./server/target/server-1.0-SNAPSHOT.jar
Hello World!
;

Layered jars

Spring boot provides the default 4 layers on jars:

  • dependencies: dependencies from third parties
  • snapshot-dependencies: snapshot dependencies from third parties
  • resources: static resources
  • application: application code and resources

to examine the layers of any layered jar (example using Spring Boot):

; java -Djarmode=layertools -jar ./server/target/server-1.0-SNAPSHOT.jar list
dependencies
spring-boot-loader
snapshot-dependencies
application
;

to extract the layers of any layered jar:

; java -Djarmode=layertools -jar ./server/target/server-1.0-SNAPSHOT.jar extract
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment