When the project is finished and the package is released, the application (application) project and the Eclipse Plug-in project (plugin) are packaged differently, and this chapter describes the packaging method for both, and gives a practical example of packaging.
7.1 Package and release of application projects
7.1.1 Introduction
Java Application project can be run out of Eclipse, to run the program first to hit it into a jar package, it is packaged in most of the methods and standard Java awt/swing Packaging method, there are several main points
L MANIFEST. MF-packing list. It is a key packaged file, primarily setting the path to the execution entry class and support library, where the Java application is run to find the Portal class and support library based on the information given in this file.
L Support Packages-if Java applications use some eclipse packages, they must also be copied to the program run directory or the program will not run. such as SWT component Support Package Swt.jar, JFace component Support package Jface.jar. These packages are to be set in the MANIFEST.MF file.
L Localized Files-if the SWT component is used, it is also necessary to copy SWT's localized file Swt-win32-3063.dll (version number 3063) to the program run directory, otherwise the program will not run. 7.1.2 Packaging Specific procedures
This section describes how to package application projects using the packaging of the Swt/jface project "MYSWT", developed in the previous chapters.
1. Edit List MANIFEST.MF
(1) Eclipse provides the Export Wizard for packaging projects, but before running this wizard, you need to create a MANIFEST.MF manifest file that reads as follows:
manifest-version:1.0
Main-class:book.chapter_4.wizard_dialog. WizardDialog1
Class-path:./lib/swt.jar./lib/jface.jar./lib/runtime.jar
Description
L Manifest-version-Specifies the version number of the manifest file
L Main-class-Specifies the entry class for the program to run. This example is set to Run the wizard dialog box developed in section 4.5.2. Note: Do not add a class extension after the name
L 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. Here Jface.jar need to use Runtime.jar package, so Runtime.jar package will also be added to Class-path.
L except for the package name and class name of the entry class, other settings are case-insensitive, such as: Class-path written as Class-path or Class-path, Swt.jar written as Swt.jar.
(2) Save the manifest file and suggest that it be placed under the root directory of the MYSWT project. Its filename can be arbitrarily taken, in this case, the Manifes.txt,eclipse Wizard automatically copies the contents of the Manifes.txt to the MANIFEST.MF file in the Meta-inf directory of the jar package when packaging.
2. Use the Eclipse Export Wizard to package the project
(1) Right-click the project name of the MYSWT project and select Export from the pop-up menu. In the dialog box that appears as shown in Figure 7.1 below, select Jar file and click Next.
Figure 7.1 Export dialog box
(2) as shown in Figure 7.2 below, 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:/myswt_application/myswt.jar" in the Select Export Destination item text box. Accept the other default settings unchanged, and 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 item is checked.
Figure 7.2 Selecting the import file
(3) as shown in Figure 7.3 below, accept the default settings unchanged and click Next.
Figure 7.3 Options for exporting classes
(4) This step is more critical. As shown in Figure 7.4 below, 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 packaging the project.
Figure 7.4 Listing File settings
After four steps above, a file named "Myswt.jar" is generated under the "D:/myswt_application" path. Myswt.jar is a zip-formatted compressed file that can be opened with WinRAR or WinZip software, which means you can also use the two software to package files instead of the Eclipse Wizard. If you use WinRAR to package the file, the compression format should select the ZIP format instead of the RAR format, and the compression rate can be arbitrarily selected.
After opening the Myswt.jar file with WinRAR, its internal directory structure is shown in Figure 7.5 below:
Figure 7.5 The internal directory structure of Myswt.jar files
There is only one file in the internal directory Meta-inf of the Myswt.jar file: MANIFEST.MF, which is the same as the contents of the manifest file Manifest.txt previously created, as follows:
manifest-version:1.0
Class-path:./lib/swt.jar./lib/jface.jar./lib/runtime.jar
Main-class:book.chapter_4.wizard_dialog. WizardDialog1
3. Copy Java application support packages and localized files
The Class-path item in the MANIFEST.MF file has three packages, which are copied from the plugins directory of Eclipse to the D:/myswt_application/lib directory. Localized files are Swt-win32-3063.dll copied to the D:/myswt_application directory. The path of the three files in Eclipse is:
Plugins/org.eclipse.swt.win32_3.0.1/ws/win32/swt.jar
Plugins/org.eclipse.jface_3.0.0/jface.jar
Plugins/org.eclipse.core.runtime_3.0.1/runtime.jar
Plugins/org.eclipse.swt.win32_3.0.1/os/win32/x86/swt-win32-3063.dll
The directory structure after the replication is completed is shown in Figure 7.6 below:
Figure 7.6 Directory structure diagram
4, write run Myswt.jar package batch process "Run.bat"
Create a batch program in the Myswt_application directory Run.bat (name, extension must be bat), its content is only a sentence, as follows:
Javaw-jar Myswt.jar
Description
L 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.
L have a nasty black command-line window to run the program, and to remove it, you can change the Run.bat content as follows: "Start Javaw-jar Myswt.jar", start refers to the "run" command that called Windows.
L If you want to place Swt-win32-3063.dll in a separate directory, such as the "d:/myswt_application/native" directory, you need to change the Run.bat content to:
Start javaw-djava.library.path=./native/-jar Myswt.jar
5. Run the program
Double-click the Run.bat file to get the program interface shown in Figure 7.7 below.
Figure 7.7 Program Operation effect diagram
6. Attention Matters
This example requires only three support packages, but your program may require more support packages to run. If you want to be in place once, you can copy all the referenced packages from the Library tab of the Java build path to the Lib directory. If you like to use a bag to add to what package, want to maintain the simplicity of packaging files, you need to go to the test: If a support package is missing, when running the program will output the error message of the not found class, from the package name in the information can know the program is missing which support package. For example, "Exception in Thread" main "Java.lang.noclassdeffounderror:org/eclipse/jface/wizard/iwizard", It is obvious from the error message that the program lacks jface packages 7.1.3 Other ways to get a jar package
To get the jar pack, in addition to the above mentioned "Export" Wizard with Eclipse, WinZip and WinRAR, and also the command line packaging software Jar.exe (located in the C:/jdk/bin directory) with Java, the Package command is:
C:/jdk/bin/jar CVFM Myswt.jar c:/eclipse3.0.1/eclipse/workspace/myswt/manifest.txt-c C:/eclipse3.0.1/eclipse/ Workspace/myswt/bin.
Description
L C:/jdk/bin/jar-because this book does not add C:/jdk/bin to the Windows environment variable path, manually specify the Jar.exe path
L Cvfm-jar.exe parameters, "C" creates a new jar package; "V" Prints debug information on the screen; "F" Specifies the generated jar file name; "M" uses the manifest file. Note that they are all lowercase
L Myswt.jar-packaged jar package name
L in the front is to put the manifest file Manifest.txt in the c:/eclipse3.0.1/eclipse/workspace/myswt/directory. If you put it in a directory with a batch file, you don't have to specify a long path.
L "-C path." Refers to packaging all the files under the path (including subdirectories), because the class file is output in the project's Bin directory, so the path is assigned to the project's Bin directory, note that the three are separated by a space, and the last character is a decimal point.
The advantage of this approach is that it is not as cumbersome as the Eclipse Export Wizard, and it is appropriate to export the jar package frequently.
7.1.4 use a Third-party plug-in to package your project
An open source organization (http://sourceforge.net/) has a plug-in that can call the Eclipse Support Pack and project compilation file into a package, called Fat Jar, and its download address is "http://fjep.sourceforge.net/ ", the detailed download no longer explains, the installation steps refer to the 1th Chapter SWT designer installation.
The use of the Fat jar is as follows:
(1) Right-click the project name of the MYSWT project and a "build fat jar" in the visible menu, as shown in Figure 7.8 below, select Build Fat jar item.
Figure 7.8 Right-click menu
(2) In the dialog box shown in Figure 7.9 below, the "Jar-name" entry is populated with the output path of the Jar package. The file list "Manifest" item is not filled in and is automatically created by default. The "Main-class" entry fills in the entry class for the program. All others accept the defaults and click Next.
Figure 7.9 Output configuration
(3) as shown in Figure 7.10 below, the support packages used by the MYSWT project are listed in the window. We only check the Runtime.jar, Swt.jar, Jface.jar these three items can be, of course, all selected and tasted, only the last jar package will be larger, because the fat jar will all support packages merged into a jar package.
Figure 7.10 Selecting the file to package
When you click the Finish button in Figure 7.10, the jar package Myswt.jar is exported to the D:/myswt_applicationh directory. As before, to run this JAR package requires a batch file as well as localized file Swt-win32-3063.dll, except that the Eclipse Support pack is no longer needed, and its directory structure is shown in Figure 7.11 below:
Figure 7.11 Directory structure
Why don't you need the Eclipse support package? That's because the support package is already in the Myswt.jar file, and from figure 7.12 below you can see that Swt.jar are broken into directories and included in the Myswt.jar package.
Figure 7.12 The internal directory structure of Myswt.jar
The contents of the MANIFEST.MF file in the Meta-inf directory are as follows, which can be found in the same place as before: Class-path.
manifest-version:1.0
Created-by:fat Jar Eclipse Plug-In
Main-class:book.chapter_4.wizard_dialog. WizardDialog1 7.1.4 allows a user's computer to not install the JRE environment
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:/myswt_application/java1.4.2" directory (java1.4.2 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 Run.bat command is "start Java1.4.2/jre/bin/javaw-jar Myswt.jar", just add a corresponding path before JAVAW.
Double-click Run.bat to run this Java application on a computer that does not have a JRE environment installed. 7.1.5 to further improve
1, discard the batch processing document (*.bat)
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 is shown in Figure 7.13 below:
Figure 7.13 Javalauncher.zip directory structure
In the table of contents in the diagram above
The L source directory contains javalauncher sources, which are written in C language.
L Changes.txt is a new version of the revised instructions
L Launch.exe is the main program
L LAUNCHER.CFG is a configuration file
L 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:
.
./java1.4.2/jre/bin/javaw.exe
-jar Myswt.jar
L The first line sets the directory that points to the jar package Myswt.jar, because Launch.exe and Myswt.jar are in one directory, so use "." is the current directory.
L The second line sets the path pointing to Jre/bin/javaw.exe. The JRE directory has been copied to the java1.4.2 subdirectory in the previous section (7.1.4 section)
After you configure launcher.cfg, double-click Launch.exe to run the Java application.
If the tenant studies how eclipse is launched, it finds that Eclipse is the same as Javalauncher: Eclipse.exe is equivalent to Launch.exe, Startup.jar is equivalent to Myswt.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.
2, beautify the icon
The icon for the Launch.exe file is too monotonous, let's give it a nice change. Changing the program icon needs to use a free software: Resource Hacker, it has Chinese version, download the URL is:
http://www.users.on.net/johnson/resourcehacker/
The steps to replace the Launch.exe icon with resource hacker are as follows:
(1) Run resource Hacker and get the window shown in Figure 7.14 below.
Figure 7.14 The main interface of Resource hacker
(2) Click the main Menu "file → open" to load the Launch.exe into the program, as shown in Figure 7.15 below.
Figure 7.15 The interface after loading the Lanunch.exe
(3) as shown above, select "icon →1→1030" on the left, then right-click "1030" and select "Replace Resource ...". As shown in Figure 7.16 below, click Open New Icon file in the pop-up window, select a satisfactory icon, and then click the Replace button.
Note: The icon file can be an EXE, DLL, RES, ICO, the software can be extracted from the EXE, DLL, res icon, in this case, select a Java icon file Javacup.ico.
Figure 7.16 Selecting an icon file
(4) as shown in Figure 7.17 below, select "file → Save As", named Myswt.exe.
Note: The choice of "save" is also possible, when resource hacker will be old Launch.exe back to Launch_original.exe. But maybe there is a problem with the refresh, in the "save" way sometimes launch.exe can not display the new icon, but sometimes.
Figure 7.17 Saving changes
(5) The final catalogue as shown in Figure 7.18, visible myswt.exe (that is, Launch.exe changed the name) icon replaced by the Java icon. Double-click Myswt.exe to run the Java application.
Figure 7.18 The final effect
3, 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 use InstallShield, Installanywhere Installer software to create a single Setup.exe file that has a wizard-style installation interface, and you can insert menu items into the Windows program bar, refer to the relevant books for the specific use of these installer software.