teach you how to pack Java code into a jar file and convert it into an EXE executable file
1. Background:
Learning Java, the textbook on the issue of the title, only a section, but also to write their own troublesome configuration file, the end result can only be converted to a jar file. It's really a heart of a bad mood. This blog teaches you how to quickly and easily pack Java code into a jar file and convert it into an EXE executable file
2. Preface:
We all know that Java can package binaries into executable jar files, double-click the jar and double click exe effect is the same, but the feeling is still different. In fact, the Java program packaged into an EXE also requires this executable jar file.
3, Prepare:
Eclipse or MyEclipse and exe4j (online with software and account downloads)
4. Specific steps:
STEP1: Packaging Java projects into executable jar files
First look at my Java project structure, Main.java is the program entry class, which has 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, as well as the project configuration file ". Classpath" and ". Project" generated by Eclipse, and click "Next"
All by default, click "Next"
In the pop-up window, select the MANIFEST.MF file we created ourselves 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 packaged the project into a exe,class-path can not be specified, in the packaging EXE also to specify the classpath.
STEP2: Package items into an EXE
Here to be clear, not all the files are packaged into an EXE, resource files can not be wrapped in, look down ↓
First, create a folder in any directory, the best name and the same project name, I created a "simulate" folder in F, and then all the resource files and the executable jar files we generated (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 the JRE, then you need to copy the JRE directory on your own machine, I installed the JRE5, the last directory structure:
Open the exe4j, skip the Welcome, and click on the second item on the left-hand side of the navigation, because we've packaged the Java project into 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 application, then select the exe-generated directory (my F:\Simulate) and click "Next"
For the exe we want to generate, if you want to generate a custom icon, then select your ico file, if you do not want to run multiple programs on a computer, you can check "Allow only a single ...", click "Next"
Click Green "+" to set the program to run the class Path
First select our own executable jar file (my Simulate.jar), click OK
Our Simulate.jar is added to the class path.
Then click 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 currently I only select the jar of my own 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 the
After the Main class setting is complete, add all the required third party libraries to class path in turn, with the same method as "Simulate.jar", click "Next"
Enter the Java minimum JRE version number, that is, the JRE below this version will not be able to run the program, then click on "Advanced Options"-"Search sequence", set up our JRE, previously said, if the client machine does not install JRE. That's okay we bundle one in
Click Green "+" to select the bundled JRE location
Select "Directiony" in the pop-up dialog box, then select the JRE folder, note: This JRE directory is that we copy the local JRE directory to the current EXE build directory, my directory is F:\Simulate\jre5, the following diagram program uses a relative directory, click OK , completing the JRE bundle operation
JRE bundle Complete, click "Next"
By default, click "Next"
By default, click "Next"
By default, click "Next"
EXE-generated
OK, finish, click "Exit" to
Let's see how it works.
The total number of our executable jars and the third-party jars is less than 5M, can jre5 directory on more than 50 m, if the target machine has a JRE environment can be deleted, the Lib directory is a third-party jar file, is now packaged into an EXE, so it does not exist reason, then the smallest configuration is left. Simulate.exe "and" config "directory.
5, Notes
The above steps are basically the steps that you have tried, some of which can be omitted, such as Step1 the configuration file (MANIFEST) in the process of converting from Java code to a jar. MF file), the system will automatically give, is not very convenient. Another example is the binding JRE in Step2, which can be unbound. As for the rest can be more optimized and easy to step, the reader can explore their own attempts.