Method for generating JAR files

xiaoxiao2021-03-06  143

Peter Qian 13-08-04

Jar - Java Archive File, the name of the name, it is a document format for Java. Almost the JAR file is zip file, and their difference is in the contents of the JAR file, containing a meta-inf / manifest.mf file, which can be automatically created when generating JAR files; Writing.

For example, there is a procedure:

Public class helloworld {public static void main (string [] args) {system.out.println ("Hello, World!");}} can be packaged by the following method: JAR CVF HelloWorld.jar HelloWorld.class where CVF is Parameters, as shown by the usage of JAR below.

Usage: jar {ctxu} [vfm0mi] [jar- file] [manifest-file] [-c directory] file name ... option: -c Create a new archive -T list of the list of archived contents -X Expand Named (or all) file -U update existing archive -V generation detailed output to standard output -f specified archive file name -M contains flag information from the indicate file - 0 only storage method; unused zip compression Format -M does not generate a list of all items (Manifest) file-i generates index information -c to change to the specified directory for the specified JAR file, and contains the following file: If a file name is a directory, it will be recursively processed. The list (Manifest) file name and archive file name need to be specified, press the same order as specified by 'm' and 'f' flags.

Example 1: Archive two Class files into an archive file called 'classes.jar': JAR CVF Classes.jar foo.class bar.class

Example 2: Archive all files in the foo / directory in the archive file named 'classes.jar' with a list of existing lists: JAR CVFM Classes.jar mymanifest -c foo /. If Use WinRAR to open HelloWorld.jar to see, you can find that in addition to helloworld.class, there are meta-inf / manifest.mf files.

In fact, we can write a manifest.mf file yourself, add main-class or class-path, which we can pack a large program, and can run the program directly through java -jar {jarname.jar}. .

For example, the program above, write a meta-inf / manifest.mf file: manifest-version: 1.0mAin-class: helloworldcreated-by: 1.4.2_03 (Sun Microsystems Inc.)

Using the command: JAR CVFM Hello.jar meta-inf / manifest.mf HelloWorld.class output results are as follows: MANIFEST Add: HelloWorld.class (read = 452) (write = 300) (33% compressed)

转载请注明原文地址:https://www.9cbs.com/read-127522.html

New Post(0)