Teach you how to package Java code into a jar file and convert it to an EXE executable

Source: Internet
Author: User

This article reproduces the address:

http://blog.csdn.net/sunkun2013/article/details/13167099

1. Background:

When you learn Java, there is only one section in the textbook about title, and you have to write your own cumbersome configuration file, and the end result can only be converted to a jar file. It's really a bad heart. This blog teaches you how to quickly and easily package Java code into jar files and convert to exe executable files

2. Preface:

We all know that Java can package binaries into executable jar files, double-click the jar and double-click the EXE effect is the same, but the feeling is still different. In fact, the Java program is packaged as EXE also requires this executable jar file.

3. Preparation:

Eclipse or MyEclipse and exe4j (online with software and account download)

4, the specific steps:

STEP1: Package A Java project into an executable jar file

First look at my Java project structure, Main.java is the program entry class, there is a Main function, config directory is some configuration files, Lib is used by the third-party class library

Start packing, simulate (Java project)-right mouse button-export

Pop-up window, select "JAR file" and click "Next"

Uncheck the "config" and "Lib" directories, and the Project Profile ". Classpath" and ". Project" generated by Eclipse, click "Next"

All by default, click "Next"

In the pop-up window, select the MANIFEST.MF file that we created and click "Finish"

The contents of my MANIFEST.MF file are:

manifest-version: 1.0 main-class:bing. Main class-path:. Lib/ojdbc14.jar lib/poi-2.5.1. Jar Lib/jbcl.jar Lib/jtattoo.jar lib/ Liquidlnf.jar Created-by:kun Sun

The purpose of the executable jar file is achieved by specifying "Main-class". In fact, we finally package the project into Exe,class-path can not be specified, in the packaging EXE when also specify CLASSPATH.

STEP2: Package the project as EXE

Here to be clear, not all the files are packaged into an EXE, the resource files can not be wrapped in, look down ↓

First, create a folder in any directory, preferably named and the project name is the same, I created a "simulate" folder on the F drive, and then all the resource files and our generated executable jar files (my Simulate.jar) are copied into this folder, Config directory and Lib directory, if you are afraid to execute the program on the machine does not install JRE, then you need to copy the JRE directory on your own machine, I installed the JRE5, the last directory structure:

Open exe4j, skip the Welcome, and click the second item in the left navigation directly, as we have already packaged the Java project as an executable jar file in advance.

In the pop-up window, select "JAR in EXE mode" and click "Next"

In the new window, take a name for our app, then select the EXE build directory (my F:\Simulate) and click "Next"

For the exe we want to generate name, if you want to create a custom icon, then select your ico file, if you do not want to run more than one of your programs on a computer, you can check "Allow only a single ...", click "Next"

Click the green "+" to set the program to run the class Path

First select our own executable jar file (my Simulate.jar) and click OK

Our Simulate.jar is added to class path.

Then click on the main class selection button in general, and in the pop-up window, exe4j will automatically search for the class that contains the main method in all the jars under the current class path, and list it because I currently only select the jar for my project. And my project only Main.jar contains the main method, so at a glance, directly select it click "OK", the program entry class is set to complete

After the Main class setting is complete, add all the required third-party class libraries to class path, and then click "Next" as the "Simulate.jar".

Enter the Java minimum JRE version number, which is lower than this version of the JRE cannot run the program, then click on "Advanced Options"-"Search sequence", set up our JRE, said before, if the client does not install the JRE do? That's okay we bundle one in

Click on the Green "+" to select the bundled JRE location

Select "Directiony" in the pop-up dialog, then select the JRE folder, note: This JRE directory is we copy the JRE directory to the current EXE generated directory, my directory is F:\SIMULATE\JRE5, the program uses the relative directory, click OK, Completing the bundle operation of the JRE

After the JRE is bundled, click "Next"

By default, click "Next"

By default, click "Next"

By default, click "Next"

EXE in Build

OK, finish, click "Exit" to

Let's see what we've achieved!

Our executable jar and third-party jar in total less than 5M, can jre5 directory of more than 50 m, if the target machine has a JRE environment can delete it, the Lib directory is a third-party jar file, is now packaged in the EXE, so it does not exist for reasons, then the minimum configuration is left " Simulate.exe "and" config "Directory!

5. Remarks

The above steps are basically the steps that you have already tried, some of which can be omitted, such as the STEP1 in the process of converting from Java code to a jar without writing the configuration file (MANIFEST. MF file), the system will automatically give, is not very convenient,. Another example is the bound JRE in Step2, which can be unbound. As for the rest of the steps can be more optimized and convenient, the reader can explore their own attempts.

Teach you how to package Java code into a jar file and convert it to an EXE executable

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.