Java programs packaged into JAR packages

Source: Internet
Author: User

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
    1. 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
    1. CD bin
    2. 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
    1. Manifest-version: 1.0
    2. Created-by:rsljdkt
    3. Class-path:.
    4. 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
    1. 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
    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.