Detailed steps to package Java into an executable jar or EXE

Source: Internet
Author: User

After the Java program completes, for the Windows operating system, the habit always wants to double-click an EXE file to run the program directly, now I will step by step implementation of the process. The end result is: Do not install the JRE environment, do not install the database, directly double-click an EXE file, you can run the program

The procedure is described below:

The first step: Complete the Java GUI program

Under Eclipse, the program runs properly.

Step two: Prepare the manifest file (MANIFEST. MF)

Method 1: Directly copy the MANIFEST.MF in other executable jar files to modify
Method 2: Use IDE tools such as Eclipse to generate the jar file automatically when it is generated
MANIFEST. MF content is as follows:

Copy CodeThe code is as follows: manifest-version:1.0
Main-class:manager.ui.mainframe
Class-path:./lib/spring.jar./lib/hibernate.jar./lib/derby.jar
Splashscreen-imager:manager/resources/images/splash.jpg

Description

Manifest-version-Specifies the version number of the manifest file
Main-class-Specifies the entry class that the program runs. Note: Do not add class name extensions
Class-path-Specifies the path to the support library. “.” Refers to the program run directory, which is the directory where the exported jar package resides. The program runs based on the setting path of the Class-path item to find the support library. Each support library is separated by a space. The Hibernate,spring,derby is used here.

Attention:

1. If Java applications use some eclipse packages, they must also be copied to the program run directory, set to Class-path, or the program will not run. References to external components are also required, such as Hibernate,spring,derby .
2. In addition to the package name and class name of the Ingress class, other settings are not case-sensitive, such as: Class-path written Class-path or Class-path can also, Swt.jar written Swt.jar also line.
3. Do not add Class name extensions
4. Each line of ': ' has a space after it, such as class-path:< space >./lib/spring.jar
5.class-path. The imported jar file must not exceed 19 per line, or an error will be made. But in the unexpected is only the first 9 per row valid, so all the same type of jar file into a jar file. As my spring.jar,hibernate.jar here, Derby.jar are a combination of similar jar files.


Step three: Modify the Spring configuration file

This is written in the configuration of spring's ' sessionfactory ' bean (corresponding to Classpathxmlapplicationcontext in the program)

Copy CodeThe code is as follows:

Manager/entity/myfile.hbm.xml
Manager/entity/filetype.hbm.xml
...


Fourth step: Build the jar file using eclipse

1. Right-click the project name and select "Export" from the pop-up menu. In the dialog box that pops up as shown in, select "Jar File" and click "Next"
2. Uncheck the files you don't want on the right. Set the output path and package name of the jar package (optionally named) to "D:\manager\manager.jar" in the "Select Export Destination (destination)" Entry text box. Accept the other default settings and click Next.
Note: Although the SRC directory is selected on the left, the source file is not exported to the package unless you check the "Export Java source files and resources" item.
3. Accept the default settings, click "Next"
4. This step is more critical. As shown, select the use an existing manifest from the workspace item, enter the manifest file you created, or select the manifest file by using the browse button next to it. After you enter the manifest file, click Finish, and Eclipse begins packaging the project.
Attached: Here you can also choose generate the manifest file. But the resulting manifest file MAINFEST.MF needs to be modified.
Note: The manifest file MAINFEST.MF must be set as the second step.

Fifth step: Build a batch file that runs Manager.jar Manager.bat (This step is not possible)

In the manager directory, create a batch program Manager.bat (name any, the extension must be bat), and its content is only one sentence, as follows:
Javaw-jar Manager.jar

Description

1.JAVAW corresponds to the C:\jdk\jre\bin\javaw.exe file, if the Windows prompt command is not found, you need to add the C:\jdk\jre\bin path to the Windows environment variable path.
2. When running the program there is a nasty black command-line window, to remove it, you can change the Run.bat content as follows: "Start Javaw-jar Manager.jar", start refers to the "run" command called Windows.
3. Add a pause to the back to let you see where the specific error occurred, such as:

Copy CodeThe code is as follows: Start Javaw-jar Manager.jar
Pause
You can run the jar file by double-clicking Manager.bat.

Sixth step: The computer does not have to install the JRE environment, but also can run.

A prerequisite for running Java programs is that the user's computer must first install the JRE environment. Although installing the JRE environment is very simple, after all, there is a little bit of a flaw. Here is a method that does not need to allow the user to install the JRE environment, and the following steps are implemented:
(1) Copy the "JRE" directory from the original JDK into the "D:\manager\java" directory (Java can also be replaced by another name).
(2) Uninstall the JDK and JRE from the native computer, which means that the Java Runtime environment is not installed on this machine.
(3) Modify batch file The command in Manager.bat is "Start Java\jre\bin\javaw-jar Manager.jar", just add a relative path before JAVAW.
Double-click Manager.bat to run this Java application on a computer that does not have a JRE environment installed.

Seventh step: Discard batch file (*.bat), generate EXE file

Running a program with a batch file does not seem professional enough, though it is sufficient to run the task. But habit is like a poison once infected it is very difficult to get rid of its influence, Windows rule people have been accustomed to running the extension is EXE program, with *.bat they will feel awkward.
We can use a free applet called Javalauncher to run Java programs instead of batch files. Javalauncher's Download URL is:
Http://www.rolemaker.dk/nonRoleMaker/javalauncher/marner_java_launcher.htm
The downloaded file is a compressed package named Javalauncher.zip, the extracted directory structure is as follows:

Copy CodeThe code is as follows: Javalauncher.zip directory structure
The source directory contains Javalauncher, which is written in C language.
Changes.txt is a revised description of the new version
Launch.exe is the main program
Launcher.cfg is a configuration file
Readme.txt is a few notes and examples
We only need to Launch.exe, launcher.cfg two files, copy the two files to the directory where the packaged files are located. Launcher.cfg is a text file with only three lines of content and modifies it as follows:
. \java\jre\bin\javaw.exe
-jar Manager.jar
The first line sets the directory that points to the jar package Managger.jar, because Launch.exe and Myswt.jar are in a directory, so use "." That is the current directory.
The second line sets the path to Jre\bin\javaw.exe. The JRE directory has been copied to the Java subdirectory above
Once you have configured Launcher.cfg, double-click Launch.exe to run the Java application.
If you look at how Eclipse starts, you find Eclipse and javalauncher the same principle: Eclipse.exe is equivalent to Launch.exe,startup.jar equivalent to Manager.jar. But Eclipse.exe not like Launch.exe to have commonality, so it does not *.cfg such a configuration file, but the startup information is solidified in the Eclipse.exe.

Eighth step: Beautify the icon

The icon for the Launch.exe file is too dull, so let's change it for a good show. To change the program's icon requires a free software: Resource Hacker.

The Nineth step: final Packaging

Before sending to the user, usually with WinZip or winrar all the files into a compressed package, and then the user gets the compressed package, after extracting it to run the program, the Eclipse software is this way.
Another way is to create a single Setup.exe file with a setup program such as InstallShield, Installanywhere, which has a wizard-like installation interface, and you can insert menu items into the Windows program bar. Please refer to the relevant books for specific use of these installer software.

Detailed steps to package Java into an executable jar or EXE

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.