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.
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.
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 /
.
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.
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
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