Introduction to jar command

Chapter 5 Archive artifacts

As a prior knowledge to understand the options in the JAR file First, I will explain the specifications of the JAR file.

JAR file structure

The JAR file can contain the following elements: For each element, let's assume you have a JAR file called "sample-root.jar". スクリーンショット 2020-05-27 15.11.58.png

An overview of each is given below.

INDEX.LIST The file generated by the -i option of the jar command. In the JAR file that contains this file and the other JAR files that it depends on By indexing the location information of the stored package, It is used to speed up the processing of the class loader.

INDEX.LIST sample


JarIndex-Version: 1.0

sample-root.jar
com
com/example
com/example/app
If there are dependent JAR files, that information also follows...

MANIFEST.MF

The manifest file is a file that holds various meta information of the JAR file. It consists of a main section and individual sections, and can be set using the "attribute: value" pair method. If the same attribute is set more than once, the one in the individual section takes precedence.

#==========Main section==========
#version information(Mandatory)
Manifest-Version: 1.0
#Main attribute
#attribute:value

#Make sure there is one line between the main section and the individual sections
# ===========Individual section===========
#Attribute by entry
#attribute:value

Below are the main attributes that can be set in the manifest file.

[Main attribute]

--Manifest-Version: Defines the version of the manifest file. Only integers and periods can be set. --Created-By: Defines the Java version and vendor from which this manifest file is generated. This attribute is automatically generated by the jar tool --Signature-Version: Defines the signature version of the jar file.

Besides,

--Attributes defined for standalone applications (such as the Main-Class header) --Attributes defined for applets --Attributes defined to identify extensions --Attributes defined for extension and package versioning and sealing information

There are various attributes such as.

[Attributes by entry]

--Content-Type: Specifies the MIME type and subtype of a particular file in a JAR file. The value must be a string of the form "type / subtype".

Signed JAR file

sample-root.DSA and sample-root.SF are signature files, This article does not mention Java security tools, so I will omit them.

services directory

There is a function called a service provider to use the function of the external JAR file, A directory that stores concrete classes that interface with external JAR files. I will omit the explanation this time, so if you want to know more, please refer to the following article. Reference article: JAR file-Service provider function

jar command options

File manipulation options

--c: Newly created --u: Update --x: Extract files and directories --t: List of archive contents --f: Specify JAR file

Manifest options

--M: Do not create manifest file --For c or u: Do not create manifest file --u and if the manifest file exists: Delete the manifest file --m: Add the attribute pair of the specified manifest file --If an attribute with the same name exists: Overwrites with the specified manifest file attribute pair --If the attribute with the same name does not exist: Add the attribute pair of the specified manifest file.

Other options

--v: Display detailed information such as the operation performed by the jar command and the list of stored files. --0: Generate JAR file without ZIP compression --i : Create INDEX.LIST in the specified JAR file --- C : Temporarily move the directory From the current directory where the jar command was executed to move to the directory during processing You can specify the target by the relative path from the directory passed to the argument instead of --- J

Examples of jar commands

Create (c option)

#Create a JAR file named jarfile with the resources under inputfiles
jar c[v0M]f jarfile [-C dir] inputfiles [-Joption]
#Create a jarfile with the resources under inputfiles
#Add value to manifest(Do not delete)
jar c[v0]mf manifest jarfile [-C dir] inputfiles [-Joption]

[About the description order of m option and f option] The order of description of m option and f option is not decided, For cmf, manifest jarfile, for cfm, jarfile manifest, and so on. It is necessary to match the order of the subsequent arguments.

Update (u option)

#In the resources under inputfiles, reflect only the changed part in the jarfile and update
jar u[v0M]f jarfile [-C dir] inputfiles [-Joption]
#Only the parts that have changed in the resources under inputfiles are reflected in the jarfile.
#Update manifest(Add or remove values)
jar u[v0]mf manifest jarfile [-C dir] inputfiles [-Joption]

Extract (x option)

#Extract all directories and files in the jar file(Defrost)To do
#If inputfiles is specified, the specified target and below will be extracted.
jar x[v]f jarfile [inputfiles] [-Joption]

List display (t option)

#List all directories and files in the jar file
#If inputfiles is specified, the list below the specified target is displayed.
jar t[v]f jarfile [inputfiles] [-Joption]

Indexing (i option)

#INDEX in the specified jarfile.Create a LIST
jar i jarfile [-Joption]

Run

Below is the first folder structure. スクリーンショット 2020-05-27 18.00.56.png

Creating a JAR file

Specify the JAR file (f), output detailed information (v), Create a new JAR file called java-sample.jar (c).

#The current directory is/java-sample
$ jar cvf java-sample.jar .
Manifest added
.DS_Store is being added(Enter=6148)(Out=411)(93%Shrinked)
lib/Is being added(Enter=0)(Out=0)(0%Stored)
lib/commons-lang3-3.10.Adding jar(Enter=523372)(Out=481881)(7%Shrinked)
src/Is being added(Enter=0)(Out=0)(0%Stored)
src/com/Is being added(Enter=0)(Out=0)(0%Stored)
src/com/example/Is being added(Enter=0)(Out=0)(0%Stored)
src/com/example/app/Is being added(Enter=0)(Out=0)(0%Stored)
src/com/example/app/UseCommons.Java is being added(Enter=647)(Out=286)(55%Shrinked)
src/com/example/app/StrFactory.class is being added(Enter=308)(Out=234)(24%Shrinked)
src/com/example/app/UseCommons$CommonsHelper.class is being added(Enter=592)(Out=398)(32%Shrinked)
src/com/example/app/StrFactory.Java is being added(Enter=115)(Out=101)(12%Shrinked)
src/com/example/app/UseCommons.class is being added(Enter=732)(Out=441)(39%Shrinked)

Java-sample.jar is created in the current directory. スクリーンショット 2020-05-27 18.07.51.png

List JAR files

#The current directory is/java-sample
$ jar tf java-sample.jar
META-INF/
META-INF/MANIFEST.MF
.DS_Store
lib/
lib/commons-lang3-3.10.jar
src/
src/com/
src/com/example/
src/com/example/app/
src/com/example/app/UseCommons.java
src/com/example/app/StrFactory.class
src/com/example/app/UseCommons$CommonsHelper.class
src/com/example/app/StrFactory.java
src/com/example/app/UseCommons.class

Unzip the JAR file

If you execute it directly under java-sample, existing files and decompressed files will be mixed, so Create a folder called unzip and unzip it there.

#The current directory is/unzip
$ jar xf ../java-sample/java-sample.jar

The decompression result of the JAR file is expanded directly under unzip. スクリーンショット 2020-05-27 18.12.10.png

Return to main page

Recommended Posts

Introduction to jar command
Introduction to JAR files
Introduction to javadoc command
Introduction to java command
Introduction to javac command
Introduction to SWING
Introduction to web3j
Introduction to Micronaut 1 ~ Introduction ~
[Java] Introduction to Java
Introduction to migration
Introduction to java
Introduction to Doma
Add files to jar files
Introduction to Ratpack (8)-Session
java jar classpath command
Introduction to RSpec 1. Test, RSpec
Introduction to Ratpack (6) --Promise
Introduction to Ratpack (9) --Thymeleaf
Introduction to PlayFramework 2.7 ① Overview
Introduction to Android Layout
Introduction to design patterns (introduction)
Introduction to Practical Programming
Introduction to Ratpack (2)-Architecture
Introduction to lambda expression
Introduction to RSpec 2. RSpec setup
Introduction to Keycloak development
Introduction to Design Patterns (Builder)
Introduction to RSpec 5. Controller specs
Introduction to RSpec 6. System specifications
Introduction to Android application development
Introduction to Ratpack (5) --Json & Registry
Introduction to Metabase ~ Environment Construction ~
Introduction to Ratpack (7) --Guice & Spring
(Dot installation) Introduction to Java8_Impression
Introduction to Design Patterns (Composite)
Introduction to Micronaut 2 ~ Unit test ~
Introduction to JUnit (study memo)
Introduction to Spring Boot ① ~ DI ~
Introduction to design patterns (Flyweight)
Introduction to Spring Boot ② ~ AOP ~
Introduction to Apache Beam (2) ~ ParDo ~
[Ruby] Introduction to Ruby Error statement
Introduction to EHRbase 2-REST API
Rbenv command to use Ruby
Introduction to design patterns Prototype
GitHub Actions Introduction to self-made actions
[Java] Introduction to Stream API
Introduction to Design Patterns (Iterator)
Introduction to Spring Boot Part 1
Introduction to Ratpack (1) --What is Ratpack?
XVim2 introduction memo to Xcode12.3
Introduction to RSpec-Everyday Rails Summary-
Introduction to Design Patterns (Strategy)
[Introduction to rock-paper-scissors games] Java
Introduction to Linux Container / Docker (Part 1)
Introduction to swift practice output Chapter5
[Introduction to Java] About lambda expressions
[Introduction to Java] About Stream API
Introduction to Functional Programming (Java, Javascript)
Introduction to algorithms with java-Shakutori method
Introduction to Design Patterns (Factory Method)