This article describes the usage of the JAR command in detail, which will be helpful for everyone to learn and summarize the use of the jar command. The details are as follows:
The JAR package is a unique compressed document in Java. In fact, everyone can understand it as a .zip package. Of course, there are differences. There is a META-INF/MANIFEST.MF file in the JAR package. When you find the JAR package, it will be automatically generated.
The JAR package is generated by the JDK installation directory /bin/jar.exe command. When we install the JDK and set the path, we can use the jar.exe command normally. It will use the classes in the lib/tool.jar tool package . Don't worry about these details.
Let's see how it is used:
1. jar command parameters:
jar command format: jar {ctxuf}[ vme 0 M i ][-C directory] file name...
Among them, one of the four parameters {ctxu} must be selected. [vfme 0 M i ] is an optional parameter, and the file name is also required.
-c creates a jar package
-t displays a list of contents in the jar
-x decompress jar package
-u adds files to the jar package
-f specifies the file name of the jar package
-v generates detailed reports and outputs them to standard devices
-m specifies the manifest.mf file. (You can make some settings for the jar package and its contents in the manifest.mf file)
-0 does not compress the contents of the jar package when generating it
-M Do not generate a manifest file (Manifest.mf) for all files. This parameter is the same as the setting that ignores the -m parameter.
-i creates an index file for the specified jar file
-C means go to the corresponding directory to execute the jar command, which is equivalent to cd to that directory, and then execute the jar command without -C
2. Jar usage examples:
(1)Create jar package
jar cf hello.jar hello
Use the test directory to generate the hello.jar package. If hello.jar exists, it will be overwritten.
(2) Create and display the packaging process
jar cvf hello.jar hello
Use the hello directory to create the hello.jar package and show an example of the creation process:
E:/>jar cvf hello.jar hello
Manifest
Add: hello/(read in = 0) (write out = 0) (0% stored)
Added: hello/TestServlet2.class (read = 1497) (write = 818) (compressed by 45%)
Added: hello/HelloServlet.class (read = 1344) (write = 736) (compressed by 45%)
Added: hello/TestServlet1.class (read = 2037) (write = 1118) (compressed by 45%)
(3) Display jar package:
jar tvf hello.jar View the contents of the hello.jar package. The specified jar package must actually exist, otherwise FileNoutFoundException will occur.
(4) Unzip the jar package:
jar xvf hello.jar
Unzip hello.jar to the current directory
(5) Add files to jar:
jar uf hello.jar HelloWorld.java
Add HelloWorld.java to hello.jar package
(6) Create uncompressed content jar package:
jar cvf0 hello.jar *.class
Generate an uncompressed jar package using all .class files in the current directory
(7) Create a jar package with manifest.mf file:
jar cvfm hello.jar manifest.mf hello
The created jar package has an additional META-INF directory, and the META-INF directory has an additional manifest.mf file. As for the role of manifest.mf, it will be mentioned later.
(8) Ignore the manifest.mf file:
jar cvfM hello.jar hello
The generated jar package does not include the META-INF directory and manifest.mf file.
(9) Add -C application:
jar cvfm hello.jar mymanifest.mf -C hello/
It means switching to the hello directory and then executing the jar command.
(10)-i generates an index list for the jar file:
When the content in a jar package is very good, you can generate an index file for it, which seems very trouble-free.
jar i hello.jar
After executing this command, it will generate an index file named INDEX.LIST in the META-INF folder of the hello.jar package. It will generate a list with the jar package name at the top.
(11) Export the decompression list:
jar tvf hello.jar >hello.txt If you want to view the detailed process of decompressing a jar, and the jar package is large, the screen information will flash by. At this time, you can output the list to a file, and slowly appreciate!
(12)jar -cvf hello.jar hello/*
For example, the original directory structure is as follows:
hello
|---com
|---org
You originally wanted to package only the com directory and org directory, but at this time the jar command will be packaged together with hello. Everyone should pay attention to this. The compressed file generated by the jar command will include the directory after it. We should enter the hello directory and execute the jar command.
Note: The user can specify the file name manifest.mf arbitrarily, but the jar command only recognizes Manifest.mf. It will convert the file name specified by the user accordingly, so the user does not need to worry.
3.Manifest.mf file writing rules:
You must pay attention to some details when writing manifest.mf. It is very demanding. I have encountered many mistakes here. Who makes it so stingy? There is no way, so I list it here for everyone.
(1) Where there cannot be blank lines or spaces, the first line cannot be a blank line (there cannot be a blank line before the first line), there cannot be a blank line between lines, and there cannot be a blank line at the end of the line. There are spaces
(2) There must be a blank line, and the last line must be a blank line (just add a carriage return after entering your content)
(3) There must be a space
key: value must be written with a space after the semicolon
4. How to use classes in jar packages
Let’s 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
Make the above file into a jar package
Write another class to call 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
5. Create executable jar package
Sometimes when you write a program by yourself, there are a lot of classes. Over time, you don’t even know which one is the main class. Moreover, there may be a lot of pictures or other files used, which makes it confusing to read. At this time, you can consider making it Into an executable jar package...
(1) Edit the manifest.mf file and add the following line
Main-Class: MyApplet
Note: The size of Main-Class is determined, the space after the colon, and the Enter after MyApplet must be entered, and then saved.
(2) Packing
jar cvfm FirstApplet.jar manifest.mf MyApplet.class
Note: manifest.mf is specified as the class path (such as: hello.Hello) or file name (applet) where the Mani-Class: MyApplet file is stored.
(3) Use of executable jar
java -jar FirstApplet.jar
It can also be used in <applet></applet>:
<applet code=MyApplet archive=FirstApplet.jar width=200 height=100></applet>
Note: The class is not given. You can just write one. You can decide the class name and package name as you like. You can change it accordingly...
6. Extend your own classes
In the JDK installation directory/jre/lib/ext directory, SUN provides convenience for us to expand our own classes. You can type your own class files into .jar packages and place them in this directory. It is installed by the ExtClassLoader class. The class loader is responsible for loading, and the ExtClassLoader class loader is AppClassL The parent loader of the loader class loader, AppClassLoader, is mainly responsible for loading files under the CLASSPATH path, and the mechanism used in Java is to delegate the parent loader, so the class files in the jar stored in this directory do not have any settings. , the class loader can find the normal loading, isn't it very convenient, ha...
If your .jar is for applet applications, you can add the following two lines to its manifest.mf before packaging it into a jar package.
Class-Path: FirstApplet.jarClass-path: SecondApplet.jarMain-Class: MyApplet
Note: Class-path can be set to multiple items, just write the jar package name directly. Main-Class is mainly used when there are multiple .class class files in the jar. Java does not know which one is the main class, so it must be specified. If there is only one class in the jar package, of course it does not need to be specified.
The order in which Java calls classes: Classes in java/lib/ext ---> Classes specified in Manifest.mf --> Classes in the current directory --> Classes specified in set CLASSPATH.
7. Call the jar package on the URL network
(1) Generate the URL of the jar package
URL u=new URL("jar:"+"FirstAppplet.jar"+!/");
(2) Create 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");
Be sure to make sure that the Mani-Class attribute is correctly set in manifest.mf in your jar package. Again, you must pay attention to the rules.
(4) Create a 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 knowledge about the Reflection reflection mechanism. If you are interested in multiple reflection mechanisms, you can check out the relevant content in the java.lang.reflect package.
8. Tips on using JAR commands:
(1) jar creates compressed ZIP file
jar cvfM TestZIP.jar test
Just add the M parameter in order not to generate META-INF related content and then change TestZIP.jar to TestZIP.zip. Isn't it very simple....
(2) Use WinRAR to decompress the .jar file
As we have said above, the JAR file is a special compressed file, so of course it can be decompressed using some of our commonly used decompression tools. As for how to decompress it, I don’t need to tell you.
(3) Use WinRAR to generate .jar files
We have already said that the main difference between JAR packages and ZIP packages is that there is an additional META-INF directory in the JAR package. There is a manifest.mf file under the META-INF directory. We only need to create the relevant directory and compress it.
The directory structure is as follows:
TestJar
|--META-INF
|--manifest.mf
|--Related class files