This article describes the use of JAR commands in detail and helps you learn and summarize the usage of JAR commands. Specifically as follows:
A jar package is a compressed document peculiar to Java, and you can actually understand it as a. zip package. Of course there is a difference, there is a meta-inf\manifest in the jar package. MF file, it is generated automatically when you find a jar package.
The jar package is generated by the JDK installation directory \bin\jar.exe command, and when we install the JDK and set the path path, we can use the Jar.exe command normally, which uses the classes in the Lib\tool.jar Toolkit. These details will not be in the care of it.
Let's see how it's used:
A. jar Command parameters:
JAR command format: jar {c T x u f}[v m e 0 m i][-c directory] filename ...
{CTXU} These four parameters must choose one. [v F m e 0 m] is an optional parameter, and the filename is also required.
-C Create a jar package
-t display list of contents in jar
-X Decompression Jar Pack
-u Add file to jar package
-f Specifies the name of the jar package
-V generates detailed reports 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 the 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 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-C
two. Examples of jar usage:
(1) Create a jar package
Using the test directory to generate Hello.jar packages, such as the presence of Hello.jar, overrides
(2) Create and display the packaging process
Create a Hello.jar package with the Hello directory and show the creation process
Cases:
E:\>jar CVF Hello.jar Hello
Marked list (manifest)
Add: hello/(read = 0) (write = 0) (stored 0%)
Add: hello/testservlet2.class (read = 1497) (write = 818) (compressed 45%)
Add: hello/helloservlet.class (read = 1344) (write = 736) (compressed 45%)
Add: hello/testservlet1.class (read = 2037) (write = 1118) (compressed 45%)
(3) display jar package:
Jar TVF Hello.jar View the contents of the Hello.jar package
The specified jar package must be real, otherwise filenoutfoundexception will occur.
(4) Extract jar package:
Extract Hello.jar to current directory
(5) Add files in jar:
Add Helloworld.java to the Hello.jar package
(6) Create an uncompressed content jar package:
Jar Cvf0 Hello.jar *.class
Generate an uncompressed jar package with all. class files in the current directory
(7) Create a jar package with MANIFEST.MF files:
Jar CVFM Hello.jar MANIFEST.MF Hello
The jar package was created with one more Meta-inf directory, meta-inf a MANIFEST.MF file, as for the role of MANIFEST.MF, it will be mentioned later.
(8) Ignore MANIFEST.MF file:
Meta-inf directories and MANIFEST.MF files are not included in the generated jar package
(9) plus-C application:
Jar CVFM Hello.jar mymanifest.mf-c hello/
Represents a switch to the Hello directory and then executes the jar command
(a)-I generate an index list for the jar file:
When the contents of a jar package are good, you can generate an index file for it, which looks very convenient.
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 top of the jar package name.
(11) Export the decompression list:
Jar TVF Hello.jar >hello.txt If you want to see the detailed process of extracting a jar, and the jar package is large and the screen information flashes, you can output the list to a file and enjoy it slowly!
(a) 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 org directory, and the JAR command was packaged with the Hello Eye. We should pay attention to this point. The compressed file generated by the JAR command contains the directory that is behind it. We should go into the Hello directory and execute the jar command again.
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 of the corresponding conversion, this does not require user concern.
three. manifest.mf File writing 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 for everyone listed.
(1) Where there is no blank line or space
The first row cannot be a blank line (there can be no blank rows before the first row), no blank lines between rows, and no spaces at the end of the line
(2) There must be a place for the empty line
The last line is a blank line (after you've lost your content, add a carriage return to OK)
(3) There must be spaces in the place
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!
Public final class person
{public
static int age ()
{return
}
}
Make 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
Interested readers can 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 may use a lot of pictures or other documents, see also disorderly, at this time you can consider it into an executable jar package ...
(1) Edit MANIFEST.MF file to join the following line
Note: The size of the Main-class, the space after the colon, myapplet after a certain input carriage return, and then save.
(2) Packing
Jar CVFM Firstapplet.jar MANIFEST.MF myapplet.class
Note: The MANIFEST.MF is specified as the Class path to hold the Mani-class:myapplet file (for example, hello.) Hello) or file name (applet)
(3) The use of executable jar
can also be used in <applet></applet>:
<applet code=myapplet archive=firstapplet.jar width=200 height=100>
</applet>
Note: The class does not give, everyone casually write a line, the class name package name themselves arbitrarily, the corresponding changes can be ...
Six. Expand your own class
In the JDK installation directory \jre\lib\ext directory, Sun for us to expand their class to provide convenience, You can make your own class files into. jar package placed in this directory, it is loaded by the Extclassloader class loader, Extclassloader class loader is the Appclassloader class loaders parent loader, appclassloader mainly responsible for loading Classpat H Path of the file, and in Java is the application of the parent loader mechanism, so this directory stored in the jar of the class file does not do any of the 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 the MANIFEST.MF before you make a jar package.
Class-path:firstapplet.jar
Class-path:secondapplet.jar
main-class:myapplet
Note: Class-path can set multiple entries and write the jar package name directly. Main-class mainly when there are multiple. Class class files in the jar, Java does not know that that is the main class, so specify that if there is only one class in the jar package, of course it can not be specified.
The order of the Java calling classes: Classes in Java\lib\ext--->manifest.mf specified in the class in the current directory--> the class specified in the-->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) Establish Jarurlconnection object
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 attribute is set correctly in the MANIFEST.MF in your jar package, and then emphasize that you must be aware of the rules.
(4) Create class object based on the obtained 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[]{});
Tip: The above uses the reflection reflection mechanism knowledge, everybody if the multiple reflection mechanism is interested, may view the related content in the Java.lang.reflect package.
eight. Jar Command usage tips:
(1) Jar Create compressed ZIP file
Plus m parameter to not generate Meta-inf related content
Then change the Testzip.jar to Testzip.zip, is not very simple ....
(2) Use WinRAR to extract. jar files
We have already said that the jar file is a special compressed file, so it can certainly use some of our commonly used decompression tools to solve, as to how to solve this, I do not have to say it.
(3) generating. jar Files with WinRAR
We've already said that the main difference between a jar package and a zip package is a meta-inf directory in the jar package, and a MANIFEST.MF file in the Meta-inf directory, so we just need to build the relevant directory and compress it.
The directory is structured as follows:
Testjar
|--meta-inf
|--MANIFEST.MF
| |-related class files