Three ways for MyEclipse to package Java projects into jar files

Option 1: Use the Export function that comes with Eclipse

 

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

Since it is a Java project that references a third-party jar package, it is necessary to customize the configuration file MANIFEST.MF, and create a file MANIFEST.MF under this project. The contents are 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 things to note here:

copy code

1. There is a space after Class-Path: and Main-Class:, 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 (that is, the lib file and the packaged jar file) should be created in the same directory as KillCheatFans.jar
in the same directory), and put the relevant jar package in it. Otherwise, an error of "Exception in thread "main" java.lang.NoClassDefFoundError" will occur;

3. After Main-Class is 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 Main-Class, you must press Enter (that is, the last line is a blank line), let the cursor go to the next line, so that the jar package you generate can find your main class to run,

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

Step 2: Right-click the Java project and select Export—> select JAR file—>Next

Step 3: Select the files to be packaged, unnecessary files do not need to be packaged, reduce the size of the packaged jar file, and configure the options as follows

There are several options here:

* Export generated class files and resources means to export only the generated .class files and other resource files
* Export all output folders for checked projects means to export all folders of checked projects
* Export java source file and resouces means that the exported jar package will contain your source code *.java, if you don't want to leak the source code, then don't choose this
* Export refactorings for checked projects include some refactored info files

Step 4: Select the configuration file path we customized in the first step, this step is very important, the default option cannot be used

The configuration items are explained here:

* Generate the manifest file: The system automatically generates the MANIFEST.MF file for us. If your project does not reference other class-paths, you can choose this item.
* Use existing mainfest from workspace: This is a .MF file that can be selected by us. The format is as written above, and it is selected when citing third-party packages.
* Seal content: To encapsulate the entire jar or the specified package packet.
* Main class: You can choose your program entry here, and the jar packaged in the future will be 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 with the name of your jar file. For example, my execution is as follows:

If there are some System.out.prinln statements in the jar (such as the execution results above), after running, you don't want to output them in the console but save them in a file for later viewing, you can use the following command:
java -jar KillCheatFans.jar > log.txt (There will be no output from 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 directory as the jar file, and then click to run automatically, which is more convenient.

 

Option 2: Install the Eclipse packaging plug-in Fat Jar

 

Scheme 1 is not suitable for containing many third-party jar files or containing third-party image resources, and it is too cumbersome. 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 resources of Eclipse Java Project into an executable jar file, which can easily complete various packaging tasks. We often come to package jar packages, but eclipse comes with packaging Jar does not seem to be enough. Fat Jar is a plug-in of eclipse. In particular, Fat Jar can be typed into 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 address: http://downloads.sourceforge.net /fjep/net.sf.fjep.fatjar_0.0.27.zip?modtime=1195824818&big_mirror=0
Copy the files in the extracted plugins to the plugins directory in the eclipse installation directory, and then restart eclipse.

Instructions:

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.

Option 3: Use the Export function that comes with Eclipse

runnable jar file, not jar file (my myeclipse version is 6.5 and doesn't have this runnable jar file option)

Selecting "package required libraries into generated jar" is to type the jar package required in your project into the jar file you want to type.

cmd executes the jar file: java -jar drag the jar file to be executed, press Enter, and it is ok.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325450920&siteId=291194637