Detailed steps for packaging Java into executable jars or exes

  • 2020-04-01 03:15:23
  • OfStack

Here's how:

Step 1: complete Java  GUI program

Under Eclipse, the program works fine.

Step 2: prepare the MANIFEST file (manifest.mf)

Method 1: directly copy the manifest.mf in other running JAR files for modification
Method 2: use an IDE tool, such as Eclipse, to automatically generate JAR files when they are generated
Manifest.mf reads 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 in which the program runs. Note: do not add the class extension after the class name
Class-path - specifies the Path to support the library. ". "refers to the directory where the program is running, where the exported jars are located. The program runtime looks up the supporting libraries based on the setting Path of the class-path item. Each supported library is separated by a space. Hibernate,spring, and Derby are used here.

Note:

1. If some Eclipse packages are used by Java applications, they must be copied to the program run directory and set to class-path, otherwise the program will not run.
2. Except for the package name and Class name of the entry Class, other Settings are case-insensitive. For example, it is ok to write class-path or class-path as class-path, or swt.jar as swt.jar.
3. Don't add the class extension after the class name
4. There is a space after ':' on each line, such as class-path :< Space > . / lib/spring. The jar
5. The Class - Path. Introducing the JAR file must not exceed 19 in a row, or complains. But in a surprise is only the first in a row 9 effective, placing all of the same type JAR files into a JAR file. If my spring here. The JAR, hibernate. JAR, Derby. The JAR is the combination of the same JAR file.


Step 3: modify the spring configuration file

In the spring 'sessionFactory' bean configuration is to write (and in the program ClassPathXmlApplicationContext)


manager/entity/MyFile.hbm.xml
manager/entity/FileType.hbm.xml
...


Step 4: generate JAR files using Eclipse

1. Right-click the project name and select "Export" from the pop-up menu. In the pop-up dialog shown in the figure below, select JAR file, and click Next
2. Uncheck all unwanted files on the right. In the "Select the export destination" item text box, set the output path of the JAR and the package name (optionally) to "D:\manager\manager.jar". Accept the other default Settings as they are, and click Next.
Note: although the SRC directory is selected on the left, the source files are not exported to the package unless the item "Export Java source files and resources" is checked.
3. Accept the default Settings and click "Next"
This is the key step. As shown in the figure below, select the "use existing manifest from workspace" item to enter the created manifest file, or select the manifest file by the browse button next to it. After entering the manifest file, click "Finish" and Eclipse begins to package the project.
P.S. you can also choose to Generate the manifest file here, but the generated manifest file mainfest.mf needs to be modified.
Note: the listing file mainfest.mf must be set as in step 2.

Step 5: generate the batch file manager.bat to run the manager.jar (this step may not be needed)

Create a batch program manager.bat (name is arbitrary, extension must be bat) in the manager directory, its content is only one statement, as follows:
Javaw - jar manager. The jar

Description:

1. Javaw corresponds to c:\ JDK \jre\bin\javaw.exe. 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. There is an annoying black command line window when running the program. To remove it, you can change the contents of run.bat as follows: "start javaw-jar manager.jar".
3. Add a pause to the end so you can see exactly what went wrong, such as:

start javaw -jar manager.jar
pause

Double-click manager.bat to run the JAR file.

Step 6: get your computer to run without having to install a JRE environment.

There is usually a prerequisite for running Java programs: the user's computer must first have a JRE environment installed. While installing the JRE environment is simple, it is one more step and a little bit flawed. Here is a way to do this without having the user install a JRE environment. The steps are as follows:
(1) copy the "jre" directory in the original JDK to the "D:\manager\ Java" directory (Java can also be changed to other names).
(2) uninstall the JDK and the JRE from the native so that the native JAVA runtime is not installed.
(3) modify the command in the batch file manager.bat to "start Java \jre\bin\ javaw-jar manager.jar", just add a corresponding path before javaw.
Double-click manager.bat to run the Java application on a computer that does not have a JRE environment installed.

Step 7: discard the batch file (*.bat) and generate the exe file

It may seem unprofessional to run a program with a batch file, although it is sufficient to complete the running task. But habit is like a kind of poison once you catch it, it is hard to get rid of its influence. People under Windows rule have been used to running programs with the extension EXE, and they feel uncomfortable with *.bat.
Instead of batch files, we can run Java programs using a free little program called JavaLauncher. JavaLauncher can be downloaded from:
http://www.rolemaker.dk/nonRoleMaker/javalauncher/marner_java_launcher.htm
The downloaded file is a zip file named javalaunch.zip. After unzipped, the directory structure is as follows:

JavaLauncher.zip The directory structure 
source         The directory contains JavaLauncher The source program is used C Written language 
changes.txt    It's the revision of the new version 
launch.exe     Is the main program 
launcher.cfg   It's a configuration file 
readme.txt     Some explanations and examples 

We just need two files, laundry.exe and laundry.cfg, and copy them to the directory where the packaged files are located. Launcher. CFG is a text file with only three lines of content. Modify it as follows:
. \ Java \ jre \ bin \ javaw. Exe
- jar manager. The jar
The first line sets the directory that points to the JAR package managger.jar, and since the launch.exe and myswt.jar are in the same directory, the ". "is the current directory.
The second line sets the path to point to jre\bin\javaw.exe. The jre directory has been copied to the Java subdirectory above
Once you have configured launch.cfg, double-click launch.exe to run the Java application.
If you examine how eclipse launches, you'll find that eclipse works the same way as JavaLauncher: eclipse.exe is equivalent to launch.exe, and startup.jar is equivalent to manager.jar. It's just that eclipse.exe doesn't have the generality of a launch. Exe, so it doesn't have a configuration file like *.cfg. Instead, it solidifies the startup information in eclipse.exe.

Step 8: beautify the icon

The icon for the launch.exe file is too monotonous, let's change it to a nicer one. The icon for changing programs requires a free program: Resource Hacker.

Step 9: pack last

Before sending it to the user, it is usually necessary to use WinZip or WinRAR to put all the files into a zip package, and then the user gets the zip package, it will be decompressed to run the program, Eclipse software is this way.
Another option is to create a single setup.exe file with InstallShield, InstallAnyWhere, and other install-based programs that have a wizard interface and can insert menu items into the Windows program bar.


Related articles: