Java Project Packaging method that relies on Third-party jar packages (the command line executes Java programs that rely on Third-party jar packages) __jar packaging

Source: Internet
Author: User


Actual problem encountered: a server and Client communication program (dependent on a third-party jar pack) Console execution server or client total error java.lang.NoClassDefFoundError, and the class is not defined by itself, tangled up found is a packaging error, did not find a third-party jar package, the solution is as follows:


Scenario 0: With the eclipse with the export function, exported runnable jar package, Java installation of the correct double-click can be run, or the console using the Java command to run, the simplest convenient way, self-test feasible


option one: Use Eclipse's own export function

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

Because you are packaging a Java project that references a third-party jar package, you need to customize the profile MANIFEST.MF to create a file MANIFEST.MF under the project, which reads as follows:

manifest-version:1.0
Class-path:lib/protobuf-jar-2.4.1.jar
Main-class:com.bonree.base.helloworldserver

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

Here are some points to note:

1, Class-path: and Main-class: There is a space behind, you must add, or you will pack up the failure, the error hint is: Invalid header field; multiple jar packet spaces separated by

2, Assuming that our project is packaged as Helloworldserver.jar, then, as defined above, a Lib folder (that is, the Lib file and the packaged jar file) should be established in the Helloworldserver.jar directory.
In the same directory) and put the associated jar bag 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 Helloworldserver.java, the file is packaged as package com.bonree.base; So here's to write Com.bonree.base.HelloWorldServer,
Do not add a. java suffix, the main file address error will be "Unable to find or load the main class" errors;

4, after the completion of the Main-class must return (that is, the last line is blank line), so that the cursor to the next line, so that you generate the jar bag to find your main class to run,

Otherwise, there will be an error in "There is no master manifest attribute in the jar".

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

Step 3: Select the files you want to package, the unwanted files do not have to be packaged, reduce the size of the packaged jar file, and configure the options as follows

Here are a few options:

* Export generated class files and resources represents exporting only generated. class files and other resource files * Export all
output folders for checked projects Indicates that all folders for Export-selected items
* Export Java source file and resouces indicate that the exported jar package will contain your source code *.java, if you don't want to leak the source, then don't choose this one.
Refactorings for checked projects include some refactored information files

Step 4: Select the profile path that we customized in the first step, which is important to not take the default option

Here's 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 reference other Class-path, you can choose this one.
* Use existing mainfest from workspace: This is a choice we can customize. MF file, as written in the format, refers to the third party package when selected.
* Seal content: To encapsulate the entire jar or the specified package packet.
* Main class: Here you can choose your program entry, the future package of jars is the result of your entry class implementation.

The final finish, that is, generated 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, such as my execution as follows:

If you have some SYSTEM.OUT.PRINLN statements in the jar (such as the results on the execution), do not want to output in the console but save in the file for later viewing, you can use the command:
Java-jar Helloworldserver.jar > Log.txt (there will be no output from the Command line window)
The output information is printed to Log.txt, and of course log.txt is automatically generated and located in and Helloworldserver.jar a directory.

The client-side output in the corresponding eclipse is as follows:

2. Create a new batch file, such as Start.bat, the content is: Java-jar Helloworldserver.jar, placed in the jar file under the same directory, then click Automatic operation can be more convenient.

Scenario Two: Install Eclipse packaged plug-in fat Jar

Solution one to contain more third-party jar files or contain third-party picture resources, and so it is not appropriate, too cumbersome. You can use a packaged plug-in to-fat the 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, and we often come to the jar pack, But Eclipse's own packaged jars seem to be inadequate, and fat jars are a plug-in to eclipse, especially fat jars that can be made into executable jar packs and are easier to use for other resources such as pictures, referencing outsourcing.


Installation Method:

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

2. Eclipse Plug-in Manual installation method
Download Address: 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 the project select Buile Fat Jar

Step 2: Configure the jar file storage directory, the main main file, and so on

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

The final finish, that is, to generate the jar file we want, very convenient.


Original address: http://www.cnblogs.com/lanxuezaipiao/p/3291641.html

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.