Method One: Through the jar command
Use of the jar command:
The following is a help description for 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 archive Directories
-x unzip an archived specified (or all) file
-u Update an existing archive file
-v generate verbose output in standard output
-f specify archive file name
-m contains manifest information from the specified manifest file
-e standalone application bundled into an executable jar file
Specify application entry point
-0 store only ; Do not use any ZIP compression
-M do not create a manifest file for the entry
-i for the specified JAR file generates index information
-C changes to the specified directory and contains the files
if there is any directory file, it is recursively processed.
The specified order of the manifest file name, the archive file name, and the entry name
is the same as the specified order for the "M", "F", and "E" flags.
Example 1: Archive two class files into an archive named Classes.jar:
Jar CVF Classes.jar Foo.class bar.class
Example 2: Use the existing manifest file "Mymanifest" and
Archive all files in the foo/directory to "Classes.jar":
Jar CVFM Classes.jar mymanifest-c foo/.
The following assumes that the compiled class file is in the bin directory
One, packaged into a generic jar package
Enter the following command:
CMD code
- Jar CVF Counter.jar-c bin.
Where the "-C bin" is actually telling the jar command to first CD to the bin directory, and then execute the command with no parameter "-C bin" in this directory, equivalent to:
CMD code
- CD bin
- Jar CVF Counter.jar. // "." Represents the current path
Second, packaged into a runnable jar package
To package as a runnable jar, there are two ways to create a MANIFEST.MF file manually, specify the main class in it, and use the-e parameter of the jar to specify the entry point to run the jar package (that is, the full name of the main class).
As an example of how to package Java source code line count programs, show how to package:
1. Manually create the MANIFEST.MF file:
1) First edit the MANIFEST.MF file with the following content:
MF Code
- Manifest-version: 1.0
- Created-by:rsljdkt
- Class-path:.
- Main-class:main
Description
The first line specifies the version of the manifest, and if none, the JDK default build: manifest-version:1.0
The second line indicates the created author, and if none, the JDK generates CREATED-BY:1.6.0_22 by default (Sun Microsystems INC).
The third line specifies the classpath of the main class.
Row four indicates the main class that the program is running
2) package using the jar command:
CMD code
- Jar CVFM Counter.jar MANIFEST. Mf-c bin.
Description
Parameter f: Specifies the package name after packaging.
Parameter m: Specifies a custom MANIFEST.MF manifest file, otherwise the JDK automatically generates a default manifest that does not contain main-class.
Parameter C: Specifies that a new archive file is created.
Parameter V: generates verbose output in standard output, which is optional.
2. Specify the entry point using the-e parameter:
Execute the following command:
CMD code
- Jar Cvfe Counter.jar Main-c bin.
Method Two: Use the export feature of Eclipse:
One, packaged into a generic jar package:
The steps are as follows:
1) Right-click on the item you want to package, select Export
2) in the popup window, select Java---JAR File, then click the Next button
3) in the Jar File Specification window, set the file name and location of the package, click on both sides next
4) in the Jar Manifest Specification window, set the configuration of the MANIFEST.MF manifest file,
If only packaged into a simple jar package, do not make any changes, take the default can
If packaged as an executable jar package, you can use an existing manifest file or select the main class directly
5) Click the Finish button to complete the package.
Second, packaged into a runnable jar package
The steps are as follows:
1) Right-click on the item you want to package, select Export
2) in the pop-up window, select Java-Runnable JAR File and click the Next button
3) in the Runnable JAR File Specification window, select Launch configuration and export destination
4) Click the Finish button to complete the package.
Java programs packaged into JAR packages