1. Create a new Java file First.java in the directory E:\lins\testJava, the content is as follows
package test;
public class First{
public static void main(String[] args){
int i = 1;
i++;
System.out.println("Hello World i="+i);
}
}
2. Open the cmd command window and enter the command:
//编译成class文件
E:\lins\testJava2>javac First.java
//新建test文件夹,并将生成的First.class文件拷贝进去
//测试class文件
E:\lins\testJava2>java test.First
Hello World i=2
//将test文件夹中的class文件打包jar
E:\lins\testJava2>jar -cef test.First First.jar test
//测试jar结果
E:\lins\testJava2>java -jar First.jar
Hello World i=2
E:\lins\testJava2>
illustrate:
Usage: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
Options:
-c create new archive
-t list archive directory
-x from archive extract specified (or all) files
in -u update existing archive
-v generate verbose output on standard output
-f specify archive filename
-m include manifest information from specified manifest file
-n perform Pack200 normalization after creating new archive
- e specify
application entry point for standalone application bundled into executable jar file
-0 store only; do not use any ZIP compression
-P preserve leading '/' (absolute path) and ".." (parent directory) in filename ) component
-M do not create a manifest file for the entry
-i generate index information for the specified jar file
-C change to the specified directory and include the following files
If any file is a directory, it is processed recursively.
The manifest file name, archive file name and entry point name are specified in the same order
as the 'm', 'f' and 'e' tags.
Example 1: Archive two class files into one archive called classes.jar:
jar cvf classes.jar Foo.class Bar.class
Example 2: Use an existing manifest file 'mymanifest'
and add the All files are archived in 'classes.jar':
jar cvfm classes.jar mymanifest -C foo/ .