Make Java source code into a jar package

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 lists the archive directory

-X Unzip the archived specified (or all) files

-U Update existing archive file

-V generates verbose output in standard output

-f Specifies the archive file name

-M contains inventory information from the specified manifest file

-E for standalone applications bundled into executable jar files, specifying application entry points

0 store only; do not use any ZIP compression

-M does not create an entry for the manifest file

-I generate index information for the specified jar file

-C changes to the specified directory and contains the files therein

If there is any catalog file, it is recursively processed. The specified order of manifest file names, archive file names, and entry names 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 an existing manifest file "Mymanifest" and archive all the files in the foo/directory to "Classes.jar":

Jar CVFM Classes.jar mymanifest-c foo/.

One, packaged into a generic jar package

Enter the following command:

[Plain]View Plaincopy
    1. Jar CVF Counter.jar-c bin.

Second, packaged into a runnable jar package

To package as a runnable jar, you need to specify the main class in the Manifest.mf file, as an example of how to package the Java source code line count program to demonstrate how to package:

1) First edit the MANIFEST.MF file with the following content:

[Plain]View Plaincopy
    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 is generated by default: manifest-version:1.0.

The second line indicates the created author, and if none, the JDK generates CREATED-BY:1.6.0_22 (Sun Microsystems Inc.) by default.

The third line specifies the classpath of the main class.

Row four indicates the main class that the program runs.

2) package using the jar command:

[Plain]View Plaincopy
    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.

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 and select Export.

2) in the pop-up window, select Java--JAR File and click the Next button.

3) in the Jar File Specification window, set the file name and location to be packaged and click Next on both sides.

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, if packaged into an executable jar package, You can use an existing manifest file or choose 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 and 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.

Eclipse Build Jar Package

first: generic class export jar package, I mean the ordinary class means this class contains the main method, and no other jar package is used.

1. Select the class or package you want to export in Eclipse, right-click, select Export Sub-option,

2. In the dialog box that pops up, select the Java file---Select the jar files, click Next;

3. In the text box after the jar file, select the location and name of the jar package you want to generate, and note the export generated class files and resources and export Java source files and resources in front of Tick, click Next;

4. Click the Next button two times to reach the jar Manifest specification. Note In the text box at the bottom of the main class, select the entry class for your jar package. Click Finish to finish.
You can enter the directory where your Jar is located in a DOS environment and run the Java-jar name. Jar to detect if the operation is correct.
Second, the class you want to export uses a different jar package inside. For example, you write a class connected to the database, using the database driver package Oracl.jar.
1. First export the class you want to export form a jar package, for example, called Test.jar
2. Create a new folder main, for example, in the D packing directory;
3. Copy the Test.jar and Oracl.jar to the main file, right-click Test.jar and unzip to the current folder. Put Meta-inf\manifest. MF Cut to another place (such as the desktop!) );
4. Right-click Oracl.jar and unzip to the current folder.
5. In a DOS environment, enter the main folder of the D drive and execute jar cvfm New.jar meta-inf/manifest.mf. , do not forget the last point.
6. Use the compression tool to open your newly generated New.jar, using the meta-inf\manifest you put on your desktop. MF Cover New.jar original.
You can enter the directory where your Jar is located in a DOS environment and run the Java-jar name. Jar to detect if the operation is correct.

Make Java source code into a jar package

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.