The following is the process:
Step 1: Complete the JavaGUI program
Under Eclipse, the program can run normally.
Step 2: Prepare the manifest file (MANIFEST.MF)
Method 1: Directly copy MANIFEST.MF in other running JAR files and modify them
Method 2: Use IDE tools, such as Eclipse to automatically generate JAR files
The content of MANIFEST.MF is as follows:
Copy the 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
illustrate:
Manifest-Version - Specify the version number of the manifest file
Main-Class - Specifies the entry class for the program to run. Note: Do not add class extension after class name
Class-Path - Specifies the path to the support library. "." refers to the program running directory, that is, the directory where the exported JAR package is located. When the program runs, the support library is found based on the setting path of the Class-Path item. Each support library is separated by spaces. Hibernate, spring, derby is used here.
Notice:
1. If Java applications use some Eclipse packages, then they must also be copied to the program running directory and set to Class-Path, otherwise the program will not be able to run. References to some external components also require this setting, such as hibernate, spring,derby.
2. Except for the package name and class name of the entry class, other settings are not case-specific. For example, Class-Path is also written as class-path or CLASS-PATH, and swt.jar is also written as SWT.JAR.
3. Do not add class extension after class name
4. There is a space after ':' in each line, such as Class-Path:<space>./lib/spring.jar
5. In Class-Path, the JAR files introduced must not exceed 19 lines, otherwise an error will be reported. However, what is surprising is that only the first 9 lines are valid, so all JAR files of the same type are made into a JAR file. .For example, spring.jar, hibernate.jar, derby.jar are all combinations of similar JAR files.
Step 3: Modify the spring configuration file
In the configuration of spring's 'sessionFactory' bean, it should be written like this (corresponding to ClassPathXmlApplicationContext in the program)
The code copy is as follows:
manager/entity/MyFile.hbm.xml
manager/entity/FileType.hbm.xml
...
Step 4: Use Eclipse to generate JAR file
1. Right-click the project name and select "Export" in the pop-up menu. In the dialog box shown in the following figure that pops up, 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 and package name of the JAR package (can be named arbitrarily) to "D:/manager/manager.jar". Accept other default settings unchanged and click Next.
Note: Although the src directory is selected on the left, the source files are not exported to the package unless the "Export java source files and resources" item is checked.
3. Accept the default settings unchanged and click "Next"
4. This step is more critical. As shown in the figure below, select the "Use existing manifest from workspace" item to enter the created manifest file, or you can select the manifest file through the "Browse" button next to it. After entering the manifest file, click "Finish" and Eclipse starts packaging the project.
Attachment: You can also choose Generate the manifest file here. However, the generated manifest file MAINFEST.MF needs to be modified.
Note: The manifest file MAINFEST.MF must be set as in the second step.
Step 5: Generate a batch file manager.bat running manager.jar (this step can be avoided)
Create a batch program manager.bat (the name must be bat) in the manager directory, and its content is only one sentence, as follows:
javaw -jar manager.jar
illustrate:
1.javaw corresponds to c:/jdk/jre/bin/javaw.exe file. If the windows prompt command is not found, the c:/jdk/jre/bin path needs to be added 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 content of run.bat as follows: "start javaw -jar manager.jar", start refers to calling the "run" command of windows .
3. Adding a pause behind will allow you to see where something went wrong, such as:
Copy the code as follows: start javaw -jar manager.jar
pause
Double-click manager.bat to run the JAR file.
Step 6: Make the computer run without having to install the JRE environment.
Usually, there is a prerequisite for running Java programs: the user's computer must first install the JRE environment. Although installing the JRE environment is very simple, after all, there is one more step, which is a little flaw. Here is a method that does not require users to install the JRE environment. The implementation steps are as follows:
(1) Copy the "jre" directory in the original JDK to the "D:/manager/java" directory (java can also be replaced with other names).
(2) Uninstall JDK and JRE from the machine, which means that the machine does not have a JAVA running environment installed.
(3) Modify the command in the batch file manager.bat to "start java/jre/bin/javaw -jar manager.jar", which only adds 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.
Step 7: Discard the batch file (*.bat) and generate the exe file
Running a program with a batch file seems not professional enough, although it is enough to complete the running task. But habits are like a poison that is difficult to get rid of its influence once it is infected. People under Windows have long been accustomed to running programs with EXE extensions, and they will feel awkward when using *.bat.
We can use a free applet called JavaLauncher to replace batch files to run Java programs. The download URL of JavaLauncher is:
http://www.rolemaker.dk/nonRoleMaker/javalauncher/marner_java_launcher.htm
The downloaded file is a compressed package named JavaLauncher.zip. The decompressed directory structure is as follows:
Copy the code as follows: JavaLauncher.zip directory structure
The source directory contains the source program of JavaLauncher and is written in C language.
changes.txt is the modification instructions for the new version
launch.exe is the main program
launcher.cfg is the configuration file
readme.txt is some instructions and examples
We only need two files launch.exe and launcher.cfg, and copy these two files to the directory where the packaged file is located. launcher.cfg is a text file with only three lines of content. Modify it as follows:
./java/jre/bin/javaw.exe
-jar manager.jar
The first line sets the directory pointing to the JAR package manager.jar. Since launch.exe and myswt.jar are in the same directory, "." is the current directory.
The second line sets the path to jre/bin/javaw.exe. The jre directory has been copied into the java subdirectory
After configuring launcher.cfg, double-click launch.exe to run the java application.
If you study the startup method of eclipse, you will find that the principle of eclipse and JavaLauncher is the same: eclipse.exe is equivalent to launch.exe, and startup.jar is equivalent to manager.jar. It's just that eclipse.exe is not universal like launch.exe, so it does not have a configuration file like *.cfg, but instead solidifies the startup information in eclipse.exe.
Step 8: Beautify the icons
The icon of the launch.exe file is too monotonous, let's change it to a better look. To change the program icon, you need to use a free software: Resource Hacker.
Step 9: The final packing
Before sending it to the user, you usually use WinZip or WinRAR to type all files into a compressed package. After the user gets this compressed package, he decompresses it and can run the program. This is how Eclipse software is.
Another way is to use installer production software such as InstallShield and InstallAnyWhere to create a single setup.exe file. It has a wizard-style installation interface and can also insert menu items into the program bar of Windows. For these installation programs, For specific use of the software, please refer to the relevant books.