Generate and execute Jar file of Java file belonging to package

Overview

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

environment

OS: macOS Catalina 10.15.6 JDK:14.0.1

Status

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. スクリーンショット 2020-08-26 23.06.24.png

② ** 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

Contents of each file

●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

approach

(1) Set the hierarchy in which the package is stored in the current directory

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$ 

(2) Specify that the manifest txt file and class file included in the jar generation command are included in the package.

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. スクリーンショット 2020-08-26 23.08.02.png

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.

At the end

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

Generate and execute Jar file of Java file belonging to package
The story of forgetting to close a file in Java and failing
Generate source code from JAR file with JD-GUI of Java Decompiler project
JAVA: jar, aar, view the contents of the file
Rename the package name of the existing jar file
<java> Read Zip file and convert directly to string
[Java] Types of comments and how to write them
[Java] Get the file in the jar regardless of the environment
Gzip-compress byte array in Java and output to file
How to execute WebCamCapture sample of NyARToolkit for Java
[Java] [Maven] Template of pom.xml to register 3rd party jar file in local repository
I want to find out which version of java the jar file I have is available
Setting method to link Java of Eclipse and Github / September 2017
I tried to summarize the basics of kotlin and java
Command to check the number and status of Java threads
Advantages and disadvantages of Java
How to change the contents of the jar file without decompressing
[Java] I definitely want to connect MySQL !!! ~ About getting JDBC driver and JAR file location ~
[Java] Various summaries attached to the heads of classes and members
[Java] Generate Data URI from byte string of file contents [Kotlin]
Basics of Java development ~ How to write programs (variables and types) ~
Collection of programming selection tasks to make and remember (Java basics)
Read the first 4 bytes of the Java class file and output CAFEBABE
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
I tried to summarize the methods of Java String and StringBuilder
Sample to read and write LibreOffice Calc fods file in Java 2021
How to get the length of an audio file in java
Replace the contents of the Jar file
About fastqc of Biocontainers and Java
Summary of [Java silver study] package
[Java] Input to stdin of Process
Log output to file in Java
[Java] Judgment of identity and equivalence
Notes on Java path and Package
I want to display images with REST Controller of Java and Spring!
[Java] Create a jar file with both compressed and uncompressed with the jar command
How to record JFR (Java Flight Recorder) and output a dump file
How to find out the Java version of a compiled class file
[Java] Note how to use RecyclerView and implementation of animated swipe processing.
[Java improvement case] How to reach the limit of self-study and beyond
I translated the grammar of R and Java [Updated from time to time]
How to read log4j configuration file in Java project summarized in jar file Memo
[Java] How to use the File class
After 3 months of Java and Spring training
[java] Summary of how to handle char
How to add jar file in ScalaIDE
Java implementation to create and solve mazes
[Java] Inheritance and structure of HttpServlet class
[Java] How to output and write files!
[Java / Swift] Comparison of Java Interface and Swift Protocol
[Java] [Maven3] Summary of how to use Maven3
[Java 7] Divide the Java list and execute the process
Output of the book "Introduction to Java"
[Introduction to Java] Variable declarations and types
Sample to unzip gz file in Java
[Java8] Search the directory and get the file
Summary of Java Math.random and import (Calendar)
[Java] Contents of Collection interface and List interface
Java to C and C to Java in Android Studio
Basics of java basics ② ~ if statement and switch statement ~
Discrimination of Enums in Java 7 and above