How to build an executable jar package file

Source: Internet
Author: User

It programmer development Essentials-all kinds of resources download list, history of the most IT resources, personal collection summary.


I have seen other programs written in Java can run like EXE files, it is envy. Now take a time to study how to write, hehe, the original is the use of a few commands, here simply record the steps to generate the executable jar package file.

One, CMD under the project into a jar package

1. Write Code

We write a class here, the main purpose of this class is to test the received parameters and print out the parameter values.

Package com.jar.test;

 

/**
 * Test into an executable jar package
 *
 * @authorAdministrator * */

publicclass myjarhello {

   /**
    * @paramargs
    */
   publicstaticvoid main (string[] args) {
       if (args!=null) {
           System.out.println (" Args.length:: "+ args.length);
           for (int i = 0; i < args.length; i++) {
               System.out.println ("parameter [" + i + "] Value:" + Args[i]);}
       } else {
           System.out.println ("Args is null!");}}


2. Generate Jar file with jar command

[1] Preparing the file

Export the current class file and follow the package structure into a directory, such as the directory "D:/temp/svntest", where the class file is located

(D:/temp/svntest/com/jar/test/myjarhello.class)

[2] Generating a jar file

Under DOS, switch to directory "D:/temp/svntest" and execute the following command to package all the files in the Svntest directory, including the files in the subfolders, into Hello.jar.

D:/TEMP/SVNTEST>JAR-CVF Hello.jar *

Mark List (manifest)

Added: com/(read in = 0) (write = 0) (0% stored)

Added: com/jar/(read in = 0) (write = 0) (0% stored)

Added: com/jar/test/(read in = 0) (write = 0) (0% stored)

Added: Com/jar/test/myjarhello.class (read in = 952) (write = 599) (37% compressed)

D:/temp/svntest>

Description: The specific parameters of the jar command can be typed into the jar after DOS and press ENTER to know the description of the specific parameters.

[3] Modifying the MANIFEST.MF file

Open the Hello.jar file with WinRAR software, modify the MANIFEST.MF file, add the following sentence to the file, and save it to the Hello.jar file. The meaning of this sentence is to say the default execution of the main program's entry.

Main-class:com.jar.test.myjarhello

Note: There is a space after the colon, and "Com.jar.test.MyJarHello" has a carriage return.

If the current class also relies on other jar packages, add the following code to the following line under "Main-class": Class-path:jar/axis.jar Jar/commons-discovery-0.2.jar

The above jar directory is placed in the same directory as the Hello.jar file, i.e., Axis.jar and Commons-discovery-0.2.jar are relative paths.

3. Run Hello.jar file

Enter the directory "d:/temp/svntest" under DOS command, and click the command below to see the effect.

D:/temp/svntest>java-jar Hello.jar

args.length::0

D:/temp/svntest>java-jar Hello.jar param1 param2

Args.length::2

The value of the parameter [0] is: param1

The value of the parameter [1] is: param2

D:/temp/svntest>

Second, myeclipse the project into a jar package

Select Project Right--->export ...----> select jar file under Java---->next---> SELECT save path--->next--->next---> select main Class---->finish completed

If the Java project has a third-party package, after extracting the project (for example, named Test.jar), open the Manifest.mf file under Meta-inf, modify the file, and add the Class-path as follows:

Class-path:lib\commons-collections.jar Lib\asm.jar Lib\bcmail-jdk14-132.jar

Lib\cglib-2.1.3.jar Lib\checkstyle-all-4.2.jar Lib\bcprov-jdk14-132.jar

Lib\commons-dbcp.jar Lib\commons-logging-1.0.4.jar Lib\commons-pool.jar

Attention:

(a) Create a Lib folder in the root directory, and copy the jar package in full;

(b) There are two spaces behind the colon, there will be two spaces between the jar and jar, otherwise the invalid export Jar file is reported as an error, if you want to wrap the packet, otherwise the line too long error will be reported, and the newline will be preceded by two spaces, otherwise the invalid export Jar file error is reported.

Then package the modified files all as Test.jar, so you can introduce a third-party package.



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.