Two ways that eclipse will package a Java project that references a third-party jar package into a jar file

Source: Internet
Author: User

Scenario One: The export feature that comes with eclipse

Step 1: Prepare the master manifest file "MANIFEST." MF ",

Because it is a Java project that refers to a third-party jar package, a custom configuration file, MANIFEST.MF, is required to create the file MANIFEST.MF under the project, which reads as follows:

Manifest-version:1.0class-path:lib/commons-codec.jar Lib/commons-httpclient-3.1.jar Lib/commons-logging-1.1.jar Lib/log4j-1.2.16.jar Lib/jackson-all-1.8.5.jarmain-class:main. Killcheatfans

The first line is the Mainifest version, the second line Class-path specifies the location of the foreign jar package, and the third line specifies the main Java file that we want to execute.

Here are a few things to note:

1, Class-path: and Main-class: There is a space behind, it must be added, otherwise packaging fails, the error is: Invalid header field;2, Assuming that our project has been packaged for Killcheatfans.jar, then as defined above, a Lib folder (that is, the Lib file and the packaged jar file) should be created in the same directory as the Killcheatfans.jar
in the same directory) and put the associated jar package inside. Otherwise, the "Exception in Thread" main "Java.lang.NoClassDefFoundError" error will appear, 3, Main-class is followed by the full address of the class, For example your main file is Killcheatfans.java, the file is packaged as package com.main; Then write Com.main.KillCheatFans here,
do not add. java suffix, the main file address error will appear "Cannot find or cannot load the main class" errors; 4, after writing Main-class must enter (that is, the last line is a blank line), let the cursor to the next line, So you can generate a jar package to find your main class to run,

Otherwise, there will be a "no master manifest attribute in jar" error.

Step 2: Right-click Java Project selection export-> select Jar File->next

Step 3: Select the files you want to package, do not need to package the files, reduce the size of the packaged jar file, and configure the following options

Here are a few options:

* Export generated class files and resources indicates that only the generated. class file and other resource files are exported. * Export all output folders for checked projects All folders in Project * Export Java source file and resouces to indicate that the exported jar package will contain your source code *.java, if you do not want to leak the source, then do not choose this option * Export refactorings for CH ecked projects to include some of the reconstructed information files.

Step 4: Select the profile path that we customized in the first step, which is important and cannot be used as a default option

Here is an explanation of the configuration items:

* Generate The manifest file: is the system to help us automatically generate MANIFEST.MF files, if your project does not refer to other Class-path, then you can choose this. * Use existing mainfest from workspace: This is optional we can customize. MF file, formatted as above, is selected when a third-party package is referenced. * Seal content: To encapsulate the entire jar or the specified package packet. * Main class: Here you can choose your program portal, the future packaging of the jar is your entry class execution results.

Finally finish, which generates the jar file we want.

There are two ways to run the jar file:

1. Run the command at the command line Java-jar your jar file name, for example my execution is as follows:

If there are some SYSTEM.OUT.PRINLN statements in the jar (such as the execution results above), you do not want the console output to be saved in the file at a later time, so you can use the command:
Java-jar Killcheatfans.jar > Log.txt (at this point the command-line window will not have any output)
The output information is printed to Log.txt, of course Log.txt automatically generated and located in and Killcheatfans.jar a directory.

2. Create a new batch file, such as Start.bat, the content is: Java-jar Killcheatfans.jar, put in the jar file in the same directory, and then click to automatically run, more convenient.

scenario two: Installing the Eclipse Packaging plugin fat Jar

Span style= "font-family: Official script; font-size:14px; " >     scheme one is inappropriate and cumbersome to contain more third-party jar files or contain third-party image resources. You can then use a packaged plug-in-fat Jar.

     Fat Jar Eclipse Plug-in is a gadget that can package all of the resources of Eclipse Java project into an executable jar file, and it's easy to do a variety of packaging tasks, we often come to the jar package, but Eclipse's own packaged jar doesn't seem to be enough, Fat Jars are a plug-in for eclipse, especially fat jars that can be made into executable jar packages, and are easier to use for other resources such as pictures, and for outsourcing.


Installation method:

1. Eclipse Online Update method
Help > Install New software > Add,
Name:fat Jar
Location:http://kurucz-grafika.de/fatjar

2. The Eclipse Plugin manual installation method
: http://downloads.sourceforge.net/fjep/net.sf.fjep.fatjar_0.0.27.zip?modtime=1195824818&big_mirror=0
Copy the files from the extracted plugins to the plugins directory in the Eclipse installation directory, and then restart Eclipse.

How to use:

Step 1: Right-click Project selection Buile Fat Jar

Step 2: Configure the jar file storage directory, main main file, etc., such as

Step 3: Select the third-party jar package you want to use

The final finish, which generates the jar file we want, is very handy.

My Shop url:http://alexia.taobao.com

Welcome everyone!!!

Eclipse two ways to package a Java project that references a third-party jar package into a jar file

Related Article

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.