Eclipse Export Executable Jar Package

Source: Internet
Author: User

A jar is a file format that is used when you publish a Java application.

jar is the abbreviation for Java Archive file, a document format for Java, which combines multiple files into a single jar archive, and the jar is a multipurpose archive and compression tool. It is based on the zip and zlib compression format. The JAR file is very similar to the zip file, which is exactly the zip file. The only difference between a jar file and a ZIP file is that it contains a meta-inf/manifest in the contents of the jar file. MF file, this file is created automatically when the jar file is generated, here is an experiment: if there are files A.java and B.java in a directory, then the jar CF Test.jar *.java is run in this directory, The Test.jar file will be generated in this directory, open with WinRAR can see more than one Meta-inf directory, and in this directory there is a file MANIFEST.MF. This validates the above mentioned situation. The following explains why the Test.jar is meta-inf/manifest. MF file. The

jar file is actually a zip archive of the class file, and for that reason, the jar file itself does not represent the tag information for the included application. In order to provide the archived label information, Manifest is thus present. The jar file specifies a specific directory to hold the tag information: The Meta-inf directory, which contains the MANIFEST.MF file, contains a description of the contents of the jar file and provides the JVM with information about the application at run time.

Export the executable jar package and this meta-inf/manifest with Eclipse. MF files are closely related. There are two kinds of situations for meta-inf/manifest. The role of the MF file is described.

The first case: the application does not reference a third-party jar package other than the JDK. just right click on the project to select the "Export ..." menu, then select Export as JAR file, specify the directory and name after the "Customize the manifest file for the jar file." One of the most important tasks in the step is to specify the program entry that runs the jar, which is the "Main class" and then click Finish. Here in my project for example, its directory structure is as follows:

Which Test.java source code is as follows

Package Test;public class Test {public static void main (string[] args) {System.out.println ("No Other jar files.");}}
the generated jar package name is Testrun.jar. The results of the operation in the CMD window are as follows:


With WinRAR lay Testrun.jar, wherein meta-inf/manifest. The MF file reads as follows:

Manifest-version:1.0main-class:test.test
where manifest-version is used to define the version of the Manifest file, Main-class defines the entry class for the jar file, which must be an executable class that, once defined, can run the jar file through Java-jar X.jar.


what needs to be explained here is that the MANIFEST. MF file has many attribute fields , because the test project above is relatively simple, so only the manifest-version and main-class two fields are used. In the case that will be described below, you must use the Class-path field.


Second Scenario: The application references a third-party jar package that is unexpected except for the JDK. There are two new projects Testjar and TestRun. Where the Testjar project is used to provide method calls, TestRun engineering calls the method defined in the Testjar project. The Testjar Project catalogue is as follows:


Testjar.java source code is as follows:

Package Testjar;public class Testjar {public static void display (String para) {System.out.println ("echo para" + para);}}

To export the Testjar project as a common jar package, named Testjar.jar, added to the TestRun project, the TestRun directory structure is as follows:


The referenced libraries structure is included in the catalog, which contains the introduced third-party Testjar.jar package. Testrun.java source code is as follows:

Package Testrun;import Testjar.testjar;public class TestRun {public static void main (string[] args) {//TODO auto-generate D Method Stubtestjar.display ("Ggggg");}}
As in the first case, package the TestRun project into Testrunjar.jar, run the jar package, and the results are as follows:



The workaround is as follows : Open the Testrunjar.jar package and edit the meta-inf/manifest. MF file, add the Class-path property and modify the result as follows:

where the Class-path property tells the class loader to use the value path to search for and load third-party classes。

Modify the meta-inf/manifest. MF file after running the result is as follows:



PS:meta-inf/manifest. MF file has many attribute fields, and has its own syntax format, there are many resources on the Internet

For example:

http://blog.csdn.net/dw_java08/article/details/7786526
Http://www.cnblogs.com/kentyshang/archive/2012/11/28/2793843.html
http://blog.csdn.net/zhifeiyu2008/article/details/8829637
Http://blog.sina.com.cn/s/blog_9075354e0101kc37.html
Http://blog.snsgou.com/post-605.html



Eclipse Export Executable Jar Package

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.