Introduction to JAR files

In the main text, I would like to summarize from the creation of the JAR file to its use for beginners (for myself).

A JAR file is a collection of multiple Java classes and packages in a single file. A good way to create this is to use the jar command. The jar command can be used if you have the JDK installed on your PC.

1. This development environment

Create a jar file under the following conditions.

・ Machine: MacBook Air 2017
・ OS: macOS High Sierra(10.13.3)
-Java version: 9.0.4

Also, JAR files are originally useful for combining multiple Java classes and packages into one file, but for the sake of simplicity, we will handle only one package and class.

2. Create a Java class to put together in a JAR file

First, create Test.java used in this example in ~ / Documents / test /. I created test / myself. The following is the contents of the created class.

Test.java


package test;

public class Test {
 public static void main(String args[]){
    System.out.println("Hello World!");
  }
}

JAR files use .class files instead of .java files, so you need to compile them with the javac command.

test username$ javac Test.java

Since the package is specified this time, in order to execute this program with the java command and check the result, the following command should be used in the Documents / directory instead of the test / directory. You need to run.

Documents username$ java test.Test
Hello World!

You have now created a Test class in ~ / Documents / test /.

3. Create a JAR file

Then create a JAR file with the jar command. The jar command is described in detail in this site including options. ..

Now let's create a JAR file with the following jar command.

Documnets username$ jar cvf Test.jar test/

As a breakdown of options

・ C: Create a new JAR file ・ V: Output detailed information to the screen ・ F: Specify the JAR file name

is.

In other words

Documnets username$ jar cvf [The name of the newly created JAR file] [Java you want to include in the JAR file
Directory containing programs]

It is like that. If you execute the following command after executing this command, you should see a result like this.

Documents username$ jar tvf Test.jar
     0 Wed Jan 31 16:20:16 JST 2018 META-INF/
    88 Wed Jan 31 16:22:12 JST 2018 META-INF/MANIFEST.MF
     0 Wed Jan 31 15:06:50 JST 2018 test/
   419 Wed Jan 31 16:06:06 JST 2018 test/Test.class
   122 Wed Jan 31 15:06:50 JST 2018 test/Test.java

The JAR file is now created.

4. Add main manifest attribute to JAR file

Let's execute the JAR file immediately using the java -jar command. Then

Documents username$ java -jar Test.jar
Test.jar does not have main manifest attribute

You should get a result like this. This happens because the Main method called when executed with java -jar does not set which class the Main method is.

You need to add the main manifest attribute to MANIFEST.MF to prevent this from happening. As an additional method, create a manifest file called .mani in the directory where the JAR file exists, and then write the following contents in that file.

Main-Class: test.Test

As a breakdown, describe the class name that holds the Main method you want to call after the package to which Test.class belongs. This time, I wrote package test; in Test.java, so test is the package to which Test.class belongs.

After creating the manifest file in this way, execute the following command

Documents username$ jar uvfm Test.jar Test.mani

The main manifest attribute is now added in MANIFEST.MF. If you check the contents of MANIFEST.MF, it will be as follows.

Manifest-Version: 1.0
Created-By: 9.0.4 (Oracle Corporation)
Main-Class: test.Test 

5. Run the JAR file

At this point, you should be able to run the JAR file with the java -jar command.

Documents username$ java -jar Test.jar
Hello World!

You may not be able to execute it without specifying the classpath. In such a case, add the following statement to ~ / .profile (create the file if it does not exist)

export CLASS_PATH=/Users/username/Documents/Test.jar

The above is the basics of JAR files. I am still a beginner, so please do not hesitate to point out the text in the comments!

Recommended Posts

Introduction to JAR files
Introduction to jar command
Introduction to Ruby 2
Introduction to SWING
Introduction to web3j
Introduction to Micronaut 1 ~ Introduction ~
[Java] Introduction to Java
Introduction to migration
Introduction to java
Introduction to Doma
Introduction to Ratpack (8)-Session
Introduction to RSpec 1. Test, RSpec
Introduction to bit operation
Introduction to Ratpack (6) --Promise
Introduction to Ratpack (9) --Thymeleaf
Introduction to PlayFramework 2.7 ① Overview
Summary of jar files
Introduction to Android Layout
Introduction to design patterns (introduction)
Introduction to Practical Programming
Introduction to javadoc command
Introduction to Ratpack (2)-Architecture
Introduction to lambda expression
Introduction to java command
Introduction to RSpec 2. RSpec setup
Introduction to Keycloak development
Introduction to javac command
A story about trying hard to decompile JAR files
Introduction to Design Patterns (Builder)
Introduction to RSpec 5. Controller specs
Introduction to RSpec 6. System specifications
Introduction to Android application development
Introduction to RSpec 3. Model specs
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)
[Java] Introduction to lambda expressions
Introduction to Spring Boot ② ~ AOP ~
Introduction to Apache Beam (2) ~ ParDo ~
[Ruby] Introduction to Ruby Error statement
Introduction to EHRbase 2-REST API
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
How to rollback migration files
Introduction to RSpec-Everyday Rails Summary-
Introduction to Design Patterns (Strategy)
[Introduction to rock-paper-scissors games] Java
How to use Maven to place resource files outside the JAR
Introduction to batch files (for statements, arrays, deferred environment variables)
Introduction to Linux Container / Docker (Part 1)