Detailed steps for Java packaged into an executable jar or EXE _java

Source: Internet
Author: User

The following describes the procedure:

First step: Complete the Java GUI program

Under Eclipse, programs can function correctly.

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

Method 1: Directly copy other MANIFEST.MF in the jar file that can be run 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 Code code 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 for the program to run. Note: Do not add a class extension after the name
Class-path-Specifies the path to the support library. “.” Refers to the running directory of the program, that is, the directory where the exported jar packages are located. The program runs to find a support library based on the Class-path entry's setup path. Each support library is separated by a space. The Hibernate,spring,derby is used here.

Attention:

1. If a Java application uses some eclipse packages, the packages must also be copied to the program run directory, set to Class-path, or the program will not run. References to some external components also need to be set, such as Hibernate,spring,derby .
2. In addition to the entry class package name and class name, 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 extension
4. Each line's ': ' has a space after it, such as class-path:< space >./lib/spring.jar
5.class-path. The introduced jar file must be no more than 19 lines per line, or an error will be made. But in an unexpected way, only the first 9 of each row is valid, so make a jar file of all the same type of jar files. Like my spring.jar,hibernate.jar here, Derby.jar are a combination of similar jar files.


Step three: Modify the Spring configuration file

Write this in the configuration of the Spring ' sessionfactory ' bean (corresponding to the Classpathxmlapplicationcontext in the program)

Copy Code code as follows:

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


Step Fourth: Generate JAR files using eclipse

1. Right-click the project name and select Export from the pop-up menu. In the dialog box that appears as shown in the following illustration, select Jar file, click Next
2. Uncheck the files that are not needed on the right. Set the output path and package name (optionally named) of the jar package to "D:\manager\manager.jar" in the Select Export Target (select the destination) item text box. Accept the other default settings unchanged, click Next.
Note: On the left although the SRC directory is selected, the source file is not exported to the package unless the export Java source files and resources (exports Java sources File and Resource) item is checked.
3. Accept the default setting, click Next
4. This step is more critical. As shown in the following illustration, select the Use existing list from workspace item, enter the manifest file that 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 starts packing the project.
Attachment: Here you can also choose generate the manifest file. But the resulting manifest file MAINFEST.MF need to be modified.
Note: The manifest file MAINFEST.MF must be set in the second step.

Step Fifth: Generate a batch file Manager.bat running Manager.jar (this step can not)

Create a batch program in the Manager directory Manager.bat (name, extension must be bat), its content is only a sentence, as follows:
Javaw-jar Manager.jar

Description

1.JAVAW corresponds to the C:\jdk\jre\bin\javaw.exe file, and 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 call to the "Run" command of Windows.
3. Add a pause in the back so you can see exactly what went wrong, such as:

Copy Code code as follows:
Start Javaw-jar Manager.jar
Pause

If you double-click Manager.bat, you can run the jar file.

Sixth step: Let the computer not install JRE environment, also can run.

Typically, there is a prerequisite for running a Java program: the user's computer must first install the JRE environment. Although the installation of the JRE environment is very simple, but after all, a little bit of a flaw. Here's a way to get the user to install the JRE environment, which is implemented as follows:
(1) Copy the "JRE" directory in the original JDK to the "D:\manager\java" directory (Java can also be replaced with a different name).
(2) Uninstall the JDK and JRE from the local computer, which means the computer does not have a Java running environment installed.
(3) Modify the batch file Manager.bat command is "start Java\jre\bin\javaw-jar Manager.jar", just add a corresponding 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, although it is sufficient to complete the task. But habits are like a poison. Once contracted, it is difficult to get rid of its effects, Windows under the rule of 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 Javalauncher.zip compressed package, and the unpacked directory structure looks like this:

Copy Code code as follows:
JAVALAUNCHER.ZIP directory Structure
The source directory contains the Javalauncher, which is written in C language.
Changes.txt is the new version of the revised instructions
Launch.exe is the main program
Launcher.cfg is a configuration file
Readme.txt is a few instructions and examples

We only need to Launch.exe, launcher.cfg two files, copy these two files to the directory where the packaged files reside. Launcher.cfg is a text file with only three lines of content, which is modified 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 one directory, so use "." is the current directory.
The second line sets the path pointing to Jre\bin\javaw.exe. The JRE directory has been copied to the Java subdirectory above
After you configure launcher.cfg, double-click Launch.exe to run the Java application.
If the tenant studies how eclipse is started, it is found that eclipse works just like javalauncher: Eclipse.exe is equivalent to Launch.exe,startup.jar equivalent to Manager.jar. But Eclipse.exe is not like Launch.exe to have versatility, so it does not *.cfg such a configuration file, but the startup information is solidified in Eclipse.exe.

Eighth step: Beautify the icon

The icon for the Launch.exe file is too monotonous, let's give it a nice change. Changing the program icon requires a free software: Resource Hacker.

Nineth Step: The final packaging

Before you send it to the user, you usually use WinZip or WinRAR to make all the files into a compressed package, and then the user gets the package, unzip it and then run the program, which is the Eclipse software.
Another way is to create a single Setup.exe file using an installer such as InstallShield, Installanywhere, which has a wizard-style installation interface, and can also insert menu items into the Windows program bar. Please refer to the relevant books for the specific use of these installer software.

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.