This article is reproduced : http://www.cnblogs.com/lanxuezaipiao/p/3291641.html
Option 1: Use the Export function that comes with Eclipse
Step 1: Prepare the main manifest file “MANIFEST.MF”,
Because it is a Java project that packages and references a third-party jar package, it needs to customize the configuration file MANIFEST.MF, and create a file MANIFEST.MF under this project, the content is as follows:
Manifest-Version: 1.0
Class-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.jar
Main-Class: main.KillCheatFans
The first line is the version of MAINIFEST, the second line Class-Path specifies the location of the foreign jar package, and the third line specifies the MAIN java file we want to execute.
A few points to note here:
1, Class-Path: and Main -Class: There is a space after it, which must be added, otherwise the packaging will fail, and the error message is: Invalid header field;
2. Assuming that our project is packaged as KillCheatFans.jar, then according to the above definition, a lib folder should be created in the same directory as KillCheatFans.jar (that is, the lib file and the packaged jar file
in the same directory), and put the relevant jar package inside. Otherwise, an error of "Exception in thread "main" java.lang.NoClassDefFoundError" will appear;
3. Main-Class is followed by the full address of the class. For example, your main file is KillCheatFans.java, and the file is packaged as package com.main; then write com.main.KillCheatFans here,
Do not add the .java suffix,If the address of the main file is written incorrectly, an error of "The main class cannot be found or cannot be loaded" will appear;
4. After writing the Main-Class, be sure to press Enter (that is, the last line is a blank line), and let the cursor go to the next line, so that the jar package you generated will be�The specified package packet.
* Main class: You can choose your program entry here, and the jar that will be packaged in the future is the execution result of your entry class.
Finally Finish, that is, the jar file we want is generated.
There are two ways to run the jar file:
1. Run the command java -jar on the command line to name your jar file, for example, my execution is as follows:
If there are some System.out.prinln statements in the jar (execution results as above), after running, you don’t want to output them on the console but save them in a file for later viewing, you can use Command:
java -jar KillCheatFans.jar > log.txt (There will be no output in the command line window at this time)
The output information will be printed to log.txt, of course log.txt is automatically generated and located in the same directory as KillCheatFans.jar.
2. Create a new batch file, such as start.bat, with the content: java -jar KillCheatFans.jar, put it in the same jar file You can just click it in the directory, and then click to run automatically, which is more convenient.
Option 2: Install the Eclipse packaging plug-in Fat Jar
Option 1 It is inappropriate and too cumbersome to contain many third-party jar files or third-party image resources. At this time you can use a packaged plug-in – Fat Jar.
Fat Jar Eclipse Plug-In is a small tool that can package all the resources of Eclipse Java Project into an executable jar file, which can easily complete various packaging tasks. We often come to make jar packages, but eclipse comes with It seems that the packaged jar is not enough. Fat Jar is a plug-in of eclipse, especially Fat Jar can be packaged as an executable Jar package, and it is more convenient to use in other resources such as pictures and reference 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 URL: http://downloads.sourceforge.net/fjep/net.sf.fjep.fatjar_0.0.27.zip” margin:0px; padding:0px”> Copy the files in the decompressed plugins to the plugins directory in the eclipse installation directory, and then restart Just eclipse.
How to use:
Step 1: Right click on the project and select Buile Fat Jar
Step 2: Configure the jar file storage directory, the main Main file, etc., as shown below
Step 3: Select the third-party jar package to be used
Finally Finish, that is, the jar file we want is generated, which is very convenient.
tyle=”margin:0px; padding:0px”> Help > Install New Software > Add,
name: Fat Jar
location: http://kurucz-grafika.de/fatjar
2. Eclipse plug-in manual installation Method
Download link: http://downloads.sourceforge. net/fjep/net.sf.fjep.fatjar_0.0.27.zip”margin:0px; padding:0px”> The decompressed plugins Copy the files in the eclipse installation directory to the plugins directory, and then restart eclipse.
How to use:
Step 1: Right click on the project and select Buile Fat Jar
Step 2: Configure the jar file storage directory, the main Main file, etc., as shown below
Step 3: Select the third-party jar package to be used
Finally Finish, that is, the jar file we want is generated, which is very convenient.