[JAVA] Create a jar file with the command

Compile from Java files to classes with all commands. And create a Jar file

What is a Jar file?

The Jar is explained on the wiki below. Jar or Java Archive is a file that is compressed in Zip format by combining multiple compiled Java bytecodes and resources such as images used by them, and a tool that outputs it. That thing.

Preparation

** Directory preparation ** The directory structure of Jar is as follows, and create a directory as needed.

mkdir jar_compile01
cd jar_compile01
mkdir classes
mkdir -p src/com/demo
mkdir MATE-INFO

In the classes directory, the compiled classes are generated here. The MATE-INFO directory contains manifest files used to define extension and package related data.

** Source code preparation ** Create a Java file under src / com / demo.

# App.java
package com.demo;
public class App {
    public static void main(String[] arg) {
        System.out.println("Hello World!");
    }
}

** Manifest file preparation ** Create a manifest file under MATE-INFO.

# MANIFEST.MF
Main-Class: com.demo.App

Class compilation

Compile Java to create a class.

javac -sourcepath src -d classes src/com/demo/App.java

When you compile, the classes will be created under the classes directory.

■ javac options </ font>

                         Description
-classpath Set the user classpath and override the user classpath in the CLASSPATH environment variable. With CLASSPATH-If neither classpath is specified, the user classpath will be the current directory. See "Setting the Classpath" for more information.
-sourcepath If no option is specified, source files are searched by user classpath as well as class files.
-d directory Set the output destination directory of the class file. If the class is part of a package, javac creates directories as needed and puts the class files in subdirectories that reflect the package name. For example-d ./Specify classes and the class name is com.demo.If it is an app, the class file is./classes/com/demo/App.It will be class.
-If d is not specified, javac puts the class files in the same directory as the source files.
-Note that the directory specified by d is not automatically added to the user classpath.
-encoding Source file encoding name(EUCJIS/SJIS etc.)Is specified.-If encoding is not specified, the platform default converter will be used.
-g Generates all debug information, including local variables. By default, only line numbers and source file information are generated.
-g:none No debug information is generated.
-g:{keyword list} Generates only certain types of debug information, as specified by a comma-separated list of keywords. The following keywords are valid:
source Source file debug information
lines Line number debug information
vars Local variable debug information
-nowarn Do not display a warning message.
-O note: -The O option does not work with the current implementations of javac and oldjavac.
Optimize your code at runtime.-Specifying the O option slows down compilation and increases the size of class files created, which can result in programs that are difficult to debug.
Prior to the Java 2 SDK, javac-g option and-The O option could not be used together. Java 2 SDK v1.In 2, you can combine these options, but with the potential for unexpected consequences such as variable loss, code move or loss.-Even if you specify the O option-The depend option is automatically enabled or-The g option is no longer automatically disabled.
-sourcepath sourcepath Specifies the source code path to search for a class or interface definition. Like the user classpath, multiple entries in the source path are semicolons(;)Separate with. The source path entry can be a directory, JAR archive, or ZIP archive. If you are using a package, the local pathname in the directory or archive must reflect the package name.
If the source is found, the classes found in the classpath will be subject to automatic recompilation.
-verbose Specifies redundant output. Prints information for each class loaded and the source file to be compiled.

Create a Jar file

Create a jar file with the jar command.

jar cvfm app.jar MATE-INFO/MANIFEST.MF -C classes .
#The following is the display after executing the command
Manifest added
com/Is being added(Enter=0)(Out=0)(0%Stored)
com/demo/Is being added(Enter=0)(Out=0)(0%Stored)
com/demo/App.class is being added(Enter=421)(Out=292)(30%Shrinked)

■ jar options </ font>

option Description
-c Create a new compressed file
-t Display a list of contents
-x Decompressing compressed files
-u Update the jar file
-v Display processing details on the command line
-f Specifying the jar file name
-m Specify manifest file
-i Create index information
-o Do not compress
-M Do not create manifest file
-C Import the specified file

Run the jar

Check the Jar file structure.

jar tf app.jar
#The following is the display after executing the command
META-INF/
META-INF/MANIFEST.MF
com/
com/demo/
com/demo/App.class

Run the Jar file.

java -jar app.jar
#The following is the execution result
Hello World!

■ java options </ font>

                              Description
-client Select the Java HotSpot Client VM. This is the default behavior.
-server Select the Java HotSpot Server VM.
-classpath
-cp
Specifies a list of directories, JAR archives, and ZIP archives to look for class files. Each entry in the classpath is a colon(:)Separate with.
-classpath or-If you specify cp, the values of these options override the settings in the CLASSPATH environment variable.
-With classpath-If you do not use cp and CLASSPATH is not set, the user classpath will be the current directory.(.)Will be.
-Dproperty=value Set the value of the system property.
-jar Run the program encapsulated in the JAR file. The first argument is the name of the JAR file, not the name of the startup class. For this option to work, the JAR file manifest should read "Main.-Class:You must specify a line of the form "classname".
In classname, public static void main that functions as the starting position of the application(String[] args)Specifies the class that contains the method.
For more information on JAR files and their manifests, see the JAR Tools page and the Java Tutorial "Jar Files". With this option, the specified JAR file will be the source for all user classes and other user classpath settings will be ignored.
On Solaris 8"java -jar"JAR files that can be run with options hold a set of execute permissions. For this reason,"java -jar"It is also possible to execute without using.
-verbose
-verbose:class
Displays information about the class each time it is loaded.
-verbose:gc Report every time a garbage collection event occurs.
-verbose:jni Native methods and other Java Native Interface(JNI)Report information about the use of.
-version Display version information and exit.
-showversion Display version information and continue.
-?
-help
Show usage and exit.

Recommended Posts