Home > Java > javaTutorial > body text

Detailed explanation of usage examples of jar command in JAVA

怪我咯
Release: 2017-07-02 10:41:50
Original
2382 people have browsed it

This article mainly introduces the usage of the jar command. It is very practical. Friends who need it can refer to it.

This article describes the usage of the JAR command in detail. It is helpful for everyone to learn and summarize the use of the jar command. Helping effect. The details are as follows:

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 lib\tool. classes in the jar toolkit. Don't worry about these details.

Let us see how to use it:

1.jar command parameters:

jar command format: jar {c t x u f }[ v m e 0 M i ][-C directory]File name...

Among them, {ctxu} must select one of the four parameters. [v f m e 0 M i ] is an optional parameter, and the file name is also required.

-c Create a jar package
-t Display the content list in the jar
-x Unzip the jar package
-u Add files to the jar package
-f Specify the jar package The file name
-v generates a detailed report and outputs it to the standard device
-m specifies the manifest.mf file. (The manifest.mf file can make some settings for the jar package and its contents)
-0 Do 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 Create an index file for the specified jar file
-C means to go to the corresponding directory to execute the jar command, which is equivalent to cd to that directory, and then without - C executes the jar command

2. Jar usage example:

(1) Create a jar package

jar cf hello.jar hello
Copy after login

Use the test directory 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
Copy after login

Use the hello directory to create the hello.jar package. And display the creation process
Example:

E:\>jar cvf hello.jar hello
Copy after login

Indicate the manifest
Add: hello/(read = 0) (write = 0)(0% stored)
Add : hello/TestServlet2.class (read = 1497) (write = 818) (compressed by 45%)
Add: hello/HelloServlet.class (read = 1344) (write Out = 736) (compressed by 45%)
Added: hello/TestServlet1.class (read in = 2037) (written out = 1118) (compressed by 45%)

(3) Display the 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
Copy after login

Unzip hello.jar to the current directory

(5) Add files to the jar:

jar uf hello.jar HelloWorld.java
Copy after login

Add HelloWorld.java to the hello.jar package

(6) Create an uncompressed content jar package:

jar cvf0 hello.jar *.class
Copy after login

Use all .class files in the current directory to generate an uncompressed jar package

(7) Create a jar package with manifest.mf file:

jar cvfm hello.jar manifest.mf hello
Copy after login

created jar There is an additional META-INF directory in the package, and there is an additional manifest.mf file under META-INF. As for the role of manifest.mf, it will be mentioned later.

(8) Ignore the manifest. mf file:

jar cvfM hello.jar hello
Copy after login

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/
Copy after login

means switching to the hello directory and then executing the jar command

(10)-i generates an index list for the jar file:

When a When the content in the jar package is good, you can generate an index file for it, which seems very trouble-free.

jar i hello.jar
Copy after login

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 at the top. name.

(11) Export decompression list:

jar tvf hello.jar >hello.txt 如果你想查看解压一个jar的详细过程,而这个jar包又很大,屏幕信息会一闪而过,这时你可以把列表输出到一个文件中,慢慢欣赏!

(12)jar -cvf hello.jar hello/*

例如原目录结构如下:
hello
|---com
|---org

你本想只把com目录和org目录打包,而这时jar命令会连同hello目洋也一块打包进。这点大家要注意。jar命令生成的压缩文件会包含它后边出的目录。我们应该进入到hello目录再执行jar命令。

注意:manifest.mf这个文件名,用户可以任指定,但jar命令只认识Manifest.mf,它会对用户指定的文件名进行相应在的转换,这不需用户担心。

三.Manifest.mf文件编写规则:

manifest.mf的编写一定要注意一些细节,它是很苛刻的,我在此也载过不少跟头,谁让它这么小气呢,没办法,所以专门给大家列出来。

(1) 不能有空行和空格的地方
第一行不可以是空行( 第一行的行前不可以有空行),行与行之间不能有空行,第行的行尾不可以有空格
(2) 一定要有空行的地方
最后一行得是空行(在输完你的内容后加一个回车就OK)

(3) 一定有空格的地方
key: value 在分号后面一定要写写一个空格

四.怎样使用jar包中的类

还是写个小例子吧,这样直观!

public final class Person
{
   public static int age()
   {
   return 30;
   }
}
Copy after login
-> javac Person.java
->jar cvf person.jar Person.class
Copy after login

将上面的文件打成一个jar包

再写一个类对其进行调用:

public class MyAge
{
      public static void getAge()
   {
         System.out.println(Person.age());
      }
}
Copy after login
->javac MyAge.java
 ->java -classpath person.jar MyAge
Copy after login


感兴趣的读者可以调试一下这个程序

五.创建可执行jar包

有时自己写个程序,类一大堆,时间一长连自己都不知道那个是主类,而且有可能用到图片或其它文件一大堆,看得也乱,这时你可以考虑把它做成一个可执行jar包...

(1) 编辑manifest.mf文件加入下面一行

Main-Class: MyApplet
Copy after login

注意:Main-Class的大小定,冒号后的空格,MyApplet后一定输入回车,然后保存。

(2) 打包

jar cvfm FirstApplet.jar manifest.mf MyApplet.class
Copy after login

注意:manifest.mf指定为存放Mani-Class: MyApplet文件的class路径(如:hello.Hello)或者文件名(applet)

(3) 可执行jar的使用

java -jar FirstApplet.jar
Copy after login

也可以中使用:

<applet code=MyApplet archive=FirstApplet.jar width=200 height=100>
</applet>
Copy after login

注意:类并没有给出,大家随便写一个就行,类名包名自己随意定,相应的更改就可以...

六.扩展自己的类

在JDK的安装目录\jre\lib\ext目录下,SUN为大家为我们扩展自己类的提供了方便,大家可以将自己的类文件打成.jar包放在此目录下,它由ExtClassLoader类装器负责进行装载,ExtClassLoader类装器是AppClassLoader类装载器的父装载器,AppClassLoader主要负责加载CLASSPATH路径下的文件,而在java中采用的又是委托父装载器的机制,所以此目录下存放的jar中的类文件不做任何的设置,类装载器就可以找到正常的加载,是不是很方便啊,呵...

如果你的.jar是给applet小应用程序看的,可以在打成jar包之前,在其manifest.mf加入下面两行。

Class-Path: FirstApplet.jar
Class-path: SecondApplet.jar
Main-Class: MyApplet
Copy after login

注意:Class-path可以设置多项,直接写jar包名既可。Main-Class主要当jar中有多个.class类文件时,java并不知道那个才是主类,所以要指定,如果jar包中只有一个类当然可以不指定。

Java调用类的顺序:java\lib\ext中的类--->Manifest.mf中指定的类-->当前目录中的类-->set CLASSPATH中指定的类。

七.调用URL网络上的jar包

(1) 生成jar包的URL

URL u=new URL("jar:"+"FirstAppplet.jar"+!/");
Copy after login

(2) 建立jarURLConnection对象

JarURLConnection juc=(JarURLConnection)u.openConnection();
Copy after login

(3) 返回jar包中主类的名字

Attributes attr=juc.getMainAttributes();
String name=attr.getValue("Mani-Class");
Copy after login

一定要确保你的jar包中的manifest.mf中已正确的设置了Mani-Class属性,再强调一下一定要注意规则。

(4) 根据得到的主类名创建Class对象

Class c=Class.forName(name);
Copy after login

(5) 根据Class对象调用其main方法:

Method cm=c.getMethod("main",new Class[]{String.class});
  cm.invoke(null,new Object[]{});
Copy after login

提示:上边用到了Reflection反射机制的相关知识,大家如果多反射机制有兴趣,可查看java.lang.reflect包中的相关内容.

八.JAR命令使用技巧:

(1) jar创建压ZIP文件

jar cvfM TestZIP.jar test
Copy after login

      加M参数为了不生成META-INF相关内容
然后将TestZIP.jar改为TestZIP.zip就可以,是不是很简单....

(2) 使用WinRAR解压.jar文件

上边我们已经说过了,说JAR文件是一种特殊的压缩文件,所以它当然可以用我们常用的一些解压     缩工具来解了,至于怎么解,这就不用我说了吧。

(3) 用WinRAR生成.jar文件

我们已经说过JAR包与ZIP包主要区别就是JAR包中多一个META-INF的目录,META-INF目录下有一个manifest.mf文件,我们只要建立好相关的目录一压缩就可以了。
目录的结构如下:

      TestJar
          |--META-INF
                |--manifest.mf
          |--相关的类文件

The above is the detailed content of Detailed explanation of usage examples of jar command in JAVA. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!