A detailed description of the use of the Jar Command (reproduced)

Source: Internet
Author: User

This article describes in detail the use of JAR commands, which can help you learn and summarize the usage of JAR commands. Specific as follows:

A jar package is a compressed document that is unique to Java, and you can actually interpret it as a. zip package. Of course there is a difference, there is a meta-inf\manifest in the jar package. MF file, which is generated automatically when you find a jar package.

The jar package is generated by the JDK install directory \bin\jar.exe command, and when we install the JDK, set the path path, you can use the Jar.exe command normally, which uses the classes in the Lib\tool.jar Toolkit. These details are out of the control.

Let's see how it's used: A. jar command parameter: jar command format: jar {c T x u f}[v m e 0 m i][-c directory] File name ... where {CTXU} These four parameters must be selected first. [v F m e 0 m i] is an optional parameter and the file name is also required. -C Create a jar package-T to display the contents list in the Jar-X unzip the jar package-U add files to the jar package-F to specify the file name of the jar package-V to generate verbose reporting and output to standard device-m to specify the MANIFEST.MF file. (The jar package and its contents can be set in a MANIFEST.MF file)-0 does not compress the contents of the jar package when it is generated-M does not produce a manifest file (MANIFEST.MF) for all files. This parameter with the Ignore-m parameter setting-I creates an index file for the specified jar file-C means to go to the appropriate directory to execute the JAR command, equivalent to the CD to that directory, and then execute the JAR command without the-C two. Jar Usage Example: (1) Create Jar package

?
1 jar cf hello.jar hello

Using the test directory to generate a Hello.jar package, such as Hello.jar exists, overwrites (2) creates and displays the packaging process

?
1 jar cvf hello.jar hello

Create a Hello.jar package with the Hello directory and display the creation process example:

?
1 E:\>jar cvf hello.jar hello

Indication list (manifest) increased: hello/(read = 0) (write = 0) (0% stored) Added: Hello/testservlet2.class (read in = 1497) (write = 818) (45% compressed) Added: Hello/helloservlet.class (read in = 1344) (write = 736) (45% compressed) Added: Hello/testservlet1.class (read in = 2037) (write = 1118) (45% compressed) (3) show jar package:

Jar TVF Hello.jar View the contents of the Hello.jar package the specified jar package must be true, or filenoutfoundexception will occur. (4) Unpack the jar package:

?
1 jar xvf hello.jar

Unzip Hello.jar to add files to the current directory (5) Jar:

?
1 jar uf hello.jar HelloWorld.java

Add Helloworld.java to the Hello.jar package (6) to create the uncompressed content Jar package:

?
1 jar cvf0 hello.jar *.class

Generate an uncompressed jar package (7) with all the. class files in the current directory to create a jar package with the MANIFEST.MF file:

?
1 jar cvfm hello.jar manifest.mf hello

Created jar package more than a meta-inf directory, meta-inf to record a MANIFEST.MF file, as for the role of MANIFEST.MF, will be mentioned later. (8) Ignore MANIFEST.MF file:

?
1 jar cvfM hello.jar hello

The generated jar package does not include the Meta-inf directory and the MANIFEST.MF file (9) plus-C application:

?
1 jar cvfm hello.jar mymanifest.mf -C hello/

Represents the switch to the Hello directory and then executes the jar command (TEN)-I generates an index list for the jar file:

When the content in a jar package is good, you can generate an index file for it, which looks like a hassle.

?
1 jar i hello.jar

After executing this command, it generates an index file named Index.list under the Meta-inf folder of the Hello.jar package, which generates a list with the jar package name at the top. (11) Export Unzip list:

Jar TVF Hello.jar >hello.txt If you want to see the detailed process of extracting a jar, and the jar is very large, the screen information will flash through, you can export the list to a file, slowly enjoy! (JAR-CVF) Hello.jar hello/*
For example, the original directory structure is as follows: Hello |---com |---org you wanted to package only the COM directory and the org directory, and the jar command would be packaged with the Hello Eye. We must pay attention to this point. The compressed file generated by the jar command will contain the directory behind it. We should go into the Hello directory and execute the jar command. Note: manifest.mf This file name, the user can specify, but the jar command only know MANIFEST.MF, it will be the user-specified file name corresponding to the conversion, which does not require user concern. three. manifest.mf file preparation rules: MANIFEST.MF must pay attention to the preparation of some details, it is very harsh, I have also carried a lot of somersault, who let it so stingy, no way, so specifically to everyone listed. (1) Where there can be no blank lines and spaces the first line cannot be a blank line (the first row cannot have a blank line before the line), there cannot be a blank line between rows and rows, and there can be no spaces at the end of the line (2) Be sure to have a blank line where the last line is empty (add a carriage return after you have finished your content)

(3) Where there must be a space key:value must write a space after the semicolon four. How to use the class in the jar package or write a small example, so intuitive!

?
1234567 publicfinal class Person{   publicstatic intage()   {   return30;   }}
?
12 -> javac Person.java->jar cvf person.jar Person.class

Make the above file into a jar package and write a class to invoke it:

?
1234567 publicclass MyAge{      publicstatic voidgetAge()   {         System.out.println(Person.age());      }}
?
12 ->javac MyAge.java ->java -classpath person.jar MyAge

Interested readers can debug this program
Five. Create an executable jar package Sometimes you write a program, a lot of classes, time long even you do not know that is the main class, and it is possible to use pictures or other files a large pile, see also messy, then you can consider it into an executable jar package ... (1) Edit the MANIFEST.MF file to add the following line

?
1 Main-Class: MyApplet

Note: The size of the Main-class, the space after the colon, myapplet must enter the carriage return, and then save.

(2) Packaging

jar cvfm FirstApplet.jar manifest.mf MyApplet. class
1

Note: MANIFEST.MF is specified as the Class path that holds the Mani-class:myapplet file, such as: Hello. Hello) or file name (applet)

(3) Use of executable jar

?
1 java -jar FirstApplet.jar

can also be used in <applet></applet>:

?
12 <applet code=MyApplet archive=FirstApplet.jar width=200height=100></applet>

Note: The class does not give, everyone casually write a line, the class name package name of their own discretion, the corresponding changes can be ... Six. Expand your own class in the JDK installation directory \jre\lib\ext directory, Sun for everyone to expand our own class to provide convenience, You can make your own class files. The jar package is placed in this directory, it is loaded by the Extclassloader class loader, the Extclassloader ClassLoader is the parent loader of the Appclassloader ClassLoader, and Appclassloader is primarily responsible for loading Classpat H Path of the file, and in Java is a proxy parent loader mechanism, so this directory is stored in the jar of the class file does not make any settings, class loader can find normal loading, is not very convenient ah, oh ... If your. jar is for applet applets, you can add the following two lines to its manifest.mf before hitting the jar package.

?
123 Class-Path: FirstApplet.jarClass-path: SecondApplet.jarMain-Class: MyApplet

Note: Class-path can set multiple items, directly write jar package name can be. Main-class mainly when there are multiple. class files in the jar, Java does not know that that is the main class, so specify if only one of the classes in the jar package can be specified.   the order in which Java calls classes: Classes in Java\lib\ext--->MANIFEST.MF the class specified in the current directory, Class-->set classpath. Seven. Call the jar package (1) on the URL network to generate the URL for the jar package

?
1 URL u=newURL("jar:"+"FirstAppplet.jar"+!/");

(2) Setting up Jarurlconnection objects

?
1 JarURLConnection juc=(JarURLConnection)u.openConnection();

(3) return the name of the main class in the JAR package

?
12 Attributes attr=juc.getMainAttributes();String name=attr.getValue("Mani-Class");

Make sure that the Mani-class property is set correctly in the MANIFEST.MF in your jar package, and then emphasize that you must pay attention to the rules. (4) Create a class object based on the resulting main class name

?
1 Class c=Class.forName(name);

(5) Call its main method according to the class object:

?
12 Method cm=c.getMethod("main",newClass[]{String.class});  cm.invoke(null,newObject[]{});

Tip: The above uses the reflection reflection mechanism of the relevant knowledge, if you are interested in multi-reflection mechanism, you can view the contents of the Java.lang.reflect package. eight. Jar Command Usage tips: (1) Jar Create compressed ZIP file

?
1 jar cvfM TestZIP.jar test

Add m parameter in order not to generate Meta-inf related content and then change Testzip.jar to Testzip.zip can be, is not very simple ....

(2) Extract the. jar file using WinRAR

Above we have already said, said Jar file is a special kind of compressed file, so it can certainly use some of our commonly used decompression tool to solve, as to how to solve, this does not need me to say.

(3) generating. jar Files with WinRAR

We have said that the main difference between the jar package and the ZIP package is that there is one more Meta-inf directory in the jar package, there is a MANIFEST.MF file in the Meta-inf directory, we just need to build the relevant directory one compression. The structure of the directory is as follows:

Testjar |--meta-inf |--MANIFEST.MF |--related class files

A detailed description of the use of the Jar Command (reproduced)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.