Method 1: Use the jar command
Jar command usage:
The following is a help description of the jar command:
Usage: jar {ctxui} [vfm0me] [Jar-file] [manifest-file] [entry-point] [-C dir] files...
Options include:
-C. Create a new archive file
-T list the archive Directories
-X decompress the archived specified (or all) File
-U: update existing archive files
-V generates detailed output in standard output
-F specifies the archive file name
-M: contains the configuration information in the specified configuration file.
-E is an independent application bound to an executable JAR file, specifying the application entry point
-0: storage only; no zip compression is used
-M: do not create a list file.
-I generate index information for the specified JAR File
-C: change to the specified directory and include the files.
If any directory file exists, recursive processing is performed on it. The order of configuration file name, archive file name, and entry name is the same as that of M, F, and E.
Example 1: Archive two class files to an archive file named classes. jar:
Jar CVF classes. Jar Foo. Class bar. Class
Example 2: Use the existing inventory file "mymanifest" and archive all the files in the foo/directory to "classes. Jar:
Jar cvfm classes. Jar mymanifest-C Foo /.
I. package it into a general jar package
Run the following command:
jar cvf counter.jar -C bin .
2. Package the package into a runable jar package
To package a jar package into a runable jar package, you must specify the main class in the manifest. MF file. The following example shows how to package the Java source code statistics program:
1) edit the manifest. MF file as follows:
Manifest-Version: 1.0Created-By: rsljdktClass-Path: .Main-Class: Main
Note:
The first line specifies the version of the List. If no version is available, the JDK will generate manifest-version: 1.0 by default.
The second line specifies the creator. If no creator exists, JDK generates created-by: 1.6.0 _ 22 (Sun Microsystems Inc.) by default .).
The third row specifies the class path of the main class.
The fourth line indicates the main class for running the program.
2) use the jar command for packaging:
jar cvfm counter.jar MANIFEST.MF -C bin .
Note:
Parameter F: Specify the package name.
Parameter M: Specifies the custom manifest. MF configuration file. Otherwise, JDK automatically generates a default list that does not contain main-class.
Parameter C: Specify to create a new archive file.
Parameter V: generate detailed output in standard output. This option is optional.
Method 2: Use the export function of Eclipse:
1. Package A general jar package:
The procedure is as follows:
1) Right-click the project to be packaged and select export.
2) In the pop-up window, select Java-> JAR file and click Next.
3) In the JAR File Specification window, set the packaged file name and storage location, and click Next on both sides.
4) in the jar manifest specification window, set manifest. if you only package the configuration of the MF configuration file into a jar package, you do not need to make any changes. Use the default configuration. If you package the configuration into an executable jar package, you can use an existing manifest file or directly Select Main class.
5) Click Finish to complete packaging.
2. Package the package into a runable jar package
The procedure is as follows:
1) Right-click the project to be packaged and select export.
2) In the pop-up window, select Java-> runnable JAR file and click Next.
3) In the runnable JAR File Specification window, select launch configuration and export destination.
4) Click Finish to complete the packaging.