"Introduction to Java that is refreshingly understood, 2nd edition" Chapter 13 When I was working on Exercise 13-2 and Exercise 13-3 I stumbled upon generating the Jar file of the class file belonging to the package and executing the Jar file, so I will introduce what I did at that time in this article.
OS: macOS Catalina 10.15.6 JDK:14.0.1
Run in terminal
(1) Generate a Jar file of "Main.java" stored in the package "hoge"
//Set current directory
HIrokinoMacBook-Pro:~ user$ cd /Users/user/src/Java/sukiri1/Chapter6/hoge
//Generate jar
HIrokinoMacBook-Pro:hoge user$ jar -cvfm test.jar manifest.txt Main.class
Manifest added
Main.class is being added(Enter=417)(Out=284)(31%Shrinked)
"Test.jar" has been created in / Users / user / src / Java / sukiri1 / Chapter6 / hoge.
② ** The generated Jar cannot be executed **
//jar file execution
HIrokinoMacBook-Pro:hoge user$ java -jar test.jar
error:Main class hoge.Could not find and load Main
Cause: java.lang.ClassNotFoundException: hoge.Main
●Main.java Store Main class in hoge package
package hoge;
public class Main {
public static void main(String[] args) {
System.out.println("hello.java");
}
}
Stored in the following directory /Users/user/src/Java/sukiri1/Chapter6/hoge/Main.java
● Manifest contents
Manfest-Version: 1.0
Main-Class: hoge.Main
↓ Reference https://ameblo.jp/syosinsyaprogram/entry-11775802006.html http://daiad.webcrow.jp/java/jar.html
Initially, the current directory was set to "cd / Users / user / src / Java / sukiri1 / Chapter6 / hoge", Change to "cd / Users / user / src / Java / sukiri1 / Chapter6" (set one level up)
HIrokinoMacBook-Pro:hoge user$ cd /Users/user/src/Java/sukiri1/Chapter6
HIrokinoMacBook-Pro:Chapter6 user$
Initially, it was "jar -cvfm test.jar manifest.txt Main.class", but added "hoge /" to the fact that "manifest.txt" and "Main.class" are included in the "hoge" package. Specify as follows and execute in the terminal.
jar -cvfm test.jar hoge/manifest.txt hoge/Main.class
"Test.jar" is created in / Users / user / src / Java / sukiri1 / Chapter6.
About ①② If you generate a jar file with the current directory set in the package ("hoge" in this case), A jar will be created in which the package does not exist in the jar file. (In this case, jar without package "hoge")
Therefore, when I ran the jar with "java -jar", it was specified as "hoge.Main" in the "Main-Class:" of the manifest. The actually generated jar file does not have the "Main.class" stored in the "hoge" package, Like this time, it says "Error: Could not find and load main class hoge.Main" It was pointed out that "I couldn't find the class hoge.main?"
Now let's run the jar.
HIrokinoMacBook-Pro:Chapter6 user$ java -jar test.jar
hello.java
Succeeded.
If you get a similar error when creating and running a jar file using the manifest Is the current directory appropriate, and is the package configuration appropriate for the jar generation command? It's a good idea to review how the manifest "Main.class" is written!
Recommended Posts