Original link:
Http://blog.chinaunix.net/uid-692788-id-2681136.html
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, we can use the Jar.exe command normally,
It will use the classes in the Lib\tool.jar Toolkit. These details are out of the control.
Let's see how it's used:
One. Jar Command parameters:
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 displays a list of contents in the Jar
-X Decompression Jar Package
-u Add file to jar package
-f Specifies the file name of the jar package
-V generates detailed reporting and outputs to standard equipment
-m Specifies 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 a jar package when it is generated
-M does not produce a manifest file (MANIFEST.MF) for all files. This parameter is set with the Ignore-m parameter
-I create an index file for the specified jar file
-C means 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. Examples of jar usage:
(1) Create a jar package
$ jar CF Hello.jar Hello using the test directory to generate the Hello.jar package, such as Hello.jar exists, then overwrite
(2) Create and display the packaging process
$ jar CVF Hello.jar Hello uses the Hello directory to create the Hello.jar package and displays the creation process
Example: E:\>jar cvf Hello.jar Hello
Mark List (manifest)
Added: hello/(read in = 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) Unpacking the JAR package
$ jar XVF Hello.jar extract Hello.jar to current directory
(5) Add File in jar
$ jar UF Hello.jar Helloworld.java add Helloworld.java to the Hello.jar package
(6) Create an uncompressed content jar package
$ jar cvf0 Hello.jar *.class generates an uncompressed jar package using all the. class files in the current directory
(7) Create a jar package with a MANIFEST.MF file
$ 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
$ jar CVFM Hello.jar Hello generated jar package does not include Meta-inf directory and MANIFEST.MF file
(9) Add-C application:
$ jar CVFM Hello.jar mymanifest.mf-c hello/
Represents a switch to the Hello directory before executing the jar command
-I generate an index list for jar files
When the content in a jar package is good, you can generate an index file for it, which looks like a hassle.
$ 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.
It 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 large, the screen information will flash through,
At this point 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 just pack 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's writing must pay attention to some details, it is very harsh,
I have also carried a lot of somersault here, who let it so stingy, no way, so special to everyone listed.
(1) Where you cannot have empty lines and spaces
The first line can not be a blank line (the first row of the line can not have a blank line), the row and row can not have a blank line, the line at the end of the row can not have spaces
(2) Be sure to have a blank line.
The last line has to be a blank line (add a carriage return after you have finished your content)
(3) Where there must be a space
Key:value Be sure to write a space after the semicolon
Four. How to use classes in a jar package
Just write a small example, so intuitive!
Public final class person
{
public static int Age ()
{
return 30;
}
}
Javac Person.java
->jar CVF Person.jar person.class The above file into a jar package
Then write a class to invoke it:
public class MyAge
{
public static void Getage ()
{
System.out.println (Person.age ());
}
}
->javac Myage.java
->java-classpath Person.jar MyAge
Debug this program *******************
Five. Create an executable jar package
Sometimes I write a program, a lot of classes, a long time even I do not know that is the main class,
And it's possible to use pictures or other documents a whole bunch of it, too, to see the mess,
At this point you can consider making it an executable jar package ...
(1) Edit the MANIFEST.MF file to add the following line
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
Note: MANIFEST.MF is specified as the file name that holds Mani-class:myapplet files
(3) Use of executable jar
Java-jar Firstapplet.jar
Or
can also be used in:
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. Extend your own classes
In the \jre\lib\ext directory of the JDK's installation directory, Sun has made it easy for us to extend our class.
You can put your own class files into the. jar package in this directory, which is loaded by the Extclassloader class loader,
The Extclassloader ClassLoader is the parent loader of the Appclassloader class loader, and Appclassloader is primarily responsible for loading files under the Classpath path.
In Java, it is also the mechanism of delegating the parent loader, so the class file in the jar stored in this directory does not have 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.
Class-path:firstapplet.jar
Class-path:secondapplet.jar
Main-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 to specify, if only one class in the jar package can certainly not 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 on the URL network
(1) Generate the URL of the jar package
URL u=new url ("jar:" + "Firstappplet.jar" +!/");
(2) Setting up Jarurlconnection objects
Jarurlconnection juc= (jarurlconnection) u.openconnection ();
(3) Return the name of the main class in the JAR package
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
Class c=class.forname (name);
(5) Call its main method according to the class object:
Method Cm=c.getmethod ("main", New Class[]{string.class});
Cm.invoke (Null,new object[]{});
How to call ***************
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. Tips for using JAR commands:
(1) Jar Create compressed ZIP file
Jar CVFM Testzip.jar test plus M parameter in order not to generate Meta-inf related content
Then change the Testzip.jar to Testzip.zip can, is not very simple ....
(2) Extract the. jar file using WinRAR
As we have already said, the jar file is a special kind of compressed file, so of course it can be solved with some of our commonly used decompression tools,
As for how to solve it, I don't have to say it.
(3) generating. jar Files with WinRAR
We have said that the main difference between a jar package and a zip package is that there is one more Meta-inf directory in the jar package.
Meta-inf Directory has a MANIFEST.MF file, we just set up the relevant directory a compression can be.
The structure of the directory is as follows:
Testjar
|--meta-inf
|--manifest.mf
|--Related classes Files
Note: You should also fill in the contents of MANIFEST.MF, will not write? It's OK to follow an existing copy.
Turn: Linux under jar command with JAR package