[Java] jar _ archive operation _ memo _ 20200811

2 minute read

jar-archive operations

Basic knowledge

What is an archive?

A collection of multiple files and folders in one file

1. What is the jar command

The Windows execution program is usually a single file with the exe extension. On the other hand, the appearance of an executable program developed in Java is a set of multiple class files (.class).

In this way, in the format of “collection of multiple class files”, ・When passing the file to another developer, an accident such as omission of file transfer may occur. I also had the author actually divide the class file into smaller pieces at the development site.

――Therefore, as an archive format (see prior basic knowledge) that collects those file sets into one, JAR (Java Archive) is defined.

If you use the jar command that comes standard with the JDK, you can create multiple class files, property files, etc.  Collectively into one JAR file, or conversely expand the contents of the JAR file to the original class file etc. I can do it. (Fig. 1)

Figure 1 Comment 2020-08-11 114326.png

1. Basic usage of jar command

What is normally done with the jar command “Create archive”, “Expand archive”, “Browse archive contents”. Execute each with the following command

・Creating an archive

Jar> jar -cvf JAR file name files and folders…

・Development of archive : Jar> jar -xvf JAR file name

-Browsing archive contents

Jar> jar -tvf JAR file name

For example, if you want to create Test.jar by combining Test1.class and Test2.class, execute the following command.

jar -cvf Test.jar Test1.class Test2.class

2. Manifest file

Of course there are people who have used archive format compressed files such as zip and lha. Actually, the JAR format is almost the same as the zip format. (If the extension of the JAR file is changed to zip, it can be expanded with software that expands the zip file)

However, the JAR file describes additional information about various files included in the archive. It is common to include a manifest file.

The manifest is a text file format, and is stored in the META-INF folder in the archive. According to the rule that it is stored under the name MANIFEST.MF.

3. Benefits of using JAR files

・Efficiency of file area

Compressing reduces the required file area and improves efficiency

·Security

Since JAR files can be signed, security restrictions can be eased to some extent

If you have any incorrect knowledge, we would appreciate it if you could teach us.