Java code packaged into jars and converted to EXE

Source: Internet
Author: User

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

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:

Download Eclipse or MyEclipse and exe4j download (there are several versions of the exe4j available for download, there is always a suitable for you, if not the degree Niang).

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.

--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------

--------------------------------------------------------------brother is a dividing line----------------------------------------------------- ----------------------------------------

--------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------

Here reprinted a Netizen's use of an article exe4j:

EXE4J is a classic software that packages Java class files as. exe files, supports compiling jar class files into EXE files that can be run directly under Windows, and can also add a start wait screen

The following dialog box appears the first time you install:


It requires that the system has more than 1.5 versions of the JDK, point download online download, if your machine has a JDK, point locate find the installed JDK directory on your machine/bin/java.exe can be installed.
The following is a detailed description of the installation process:
The 1th step is the default.
2nd step, if you already have a well-made jar file, the following "jar in EXE".
For packaged jar files, right-click the project in Eclipse to export, no jar file to leave me a message


The 3rd step, above is the project name, can fill in, the following one writes out you want to be packaged exe file output directory:


4th step, because my demo program is graphics, so choose the first, if your program is the console, then select the second, executable name write the name of the EXE file you are going to generate, icon file can choose to generate the file icons


5th step, first, click the green "+" sign, archive in the pop-up window, then find the jar file that was originally done, "OK", and then return to the jar file path in the following class path, then the main Click inside the class bar to find the classes where main is located, this step is complete:


6th step, the JRE version of your system, fill in a 1.3, the following 1.6:


Of course, this configuration of the program can only be installed on the machine with the JRE, the issue is always to consider whether users have the ability to install the JRE on their own machine, can not publish a program and the user to install a JRE itself, below I will describe how to dynamically package the JDK environment variables into the EXE, Click on the Advanced options here and select Search sequence.


First copy the JRE to the exe file output directory, I use C:\xaocker\, in the popup window, delete all the items in the list, and then click Green +, select directory and select the root of the JRE (note is the copy of the directory),


Click OK, then click Advanced Options, select the preferred VM, select the client hostspot vm in the pop-up window, and click the Next button to continue:


7th, 8 step is some basic configuration, default, 9th step, 10th step you can click this to see the effect:


Exe4j unregistered version will appear the following dialog box: (the registration code can be used free of charge)


Click OK to see the results of your program!

Java.exe in accordance with a set of logic to find the JRE can be used, first find in their own directory there is no JRE, and then find their own parent directory there is no JRE; Finally, the registry that queries Windows.
Now our program can be executed on any computer, but the disadvantage is fatal, for example, my program is 160K, but wherever you go to carry more than 70 m of the JRE, it is a bit of a fuss, in fact, as our private JRE, a lot of content can be discarded. The license in the JRE directory can not be, the execution file under the bin only need to keep java.exe or javaw.exe,lib, just keep the rt,jsse,jce,charsets several libraries. In addition to the i386 and Zi two subdirectories, the remaining subdirectories are not allowed. Zi only need to keep the subdirectories of their own region and some of the files below it. LIB, except for the property files outside the library and so on are to be preserved. With this cleanup, the JRE is still close to 50MB. You can also continue to clean up a few library files do not need the content, which requires careful collation, will be very hard. It's best to write an automated tool to help us sort them out. The Java-written media player from Sun Company to JMF comes with a JRE, only a few megabytes.
After the cleanup, we need to run our application several times to make sure our JRE is not missing anything.
OK, after careful cleaning, the file becomes smaller and can be released ...

Java code packaged into jars and converted to EXE

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.