When the project is completed, the package is packaged, the application (Application) project and the Plugin package is different. This chapter will introduce both packaging methods, and give the actual package example.
I. Package and issue of application project
1.1 Introduction
After the Java application project is completed, you can get rid of Eclipse. To run the program, you must first put it into a JAR package. It is the same as a large method of packaging and the standard Java's AWT / SWING packaging method, mainly the following Point
l Manifest.mf - Package list. It is a key file that is packaged, mainly to set the path to the entry class and support library. When running the Java application, you will find the entry class and support library according to the information given in this file.
l Support Pack - If the Java application uses some Eclipse packages, you must also copy these packages to the program run directory, otherwise the program will not be able to run. Such as SWT component support package SWT.jar, JFACE component support package jface.jar. These packages must be set in the Manifest.mf file.
l Localization file - If you use the SWT component, you also need to copy the localized file SWT-WIN32-3063.DLL (3063 is the version number) of the SWT to the program run directory, otherwise the program will not be able to run.
1.2 Specific steps in packaging
This section will use the SWT / JFACE project "MySWT" package developed by the previous chapter as an example to introduce the method of the package application project.
1, edit list manifest.mf
(1) Eclipse provides an "export" wizard for packaging projects, but this example is required to create a manifest.mf list file before running this wizard. The content is 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:
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 - Specify the version number of the manifest file
l Main-class - Specifies the entry class running. This example is set to the Wizard dialog that is running 4.5.2. Note: Don't add a Class extension after class name
l Class-path - Specifies the path to the support library. "." The program runs the directory, that is, the directory where the exported JAR package is located. The program is run when the program is run according to the set path of the Class-Path item. Each support library is separated from spaces. Here JFace.jar needs to use the runtime.jar package, so the runtime.jar package is also added to the class-path.
L In addition to the package name and class name of the entrance class, other settings items are not case sensitive, such as Class-Path writes Class-Path or Class-Path, swt.jar is written into SWT.jar.
(2) Save the inventory file and suggest placed in the root of the MySWT project. Its file name can be arbitrarily taken. This example is named manifes.txt, the Eclipse wizard automatically copies the contents of Manifest.txt to the Meta-Inf file under the JAR package when packaged. 2. Use the Eclipse "Export" Wizard to Package Projects
(1) Right-click the project name of the MySWT project, and select "Export" in the pop-up menu. In the dialog box shown in Figure 7.1, select "JAR File", click Next.
Figure 1 Export dialog
(2) As shown in Figure 2, the files that are not needed on the right will be removed. Set the output path of the JAR package in the "Select Export Target" item text box (any name) to "D: /MYSWT_Application/myswt.jar". Accept other default settings unchanged, click Next.
Note: Although the SRC directory is selected on the left, the source file will not be exported to the package unless the "Export Java Source Code File and Resources" item is checked.
Figure 2 Select the import file
(3) As shown in Figure 3 below, accept the default settings unchanged, click Next.
Figure 3 Export class option
(4) This step is critical. As shown in Figure 7.4 below, select "Use an existing list from workspace" item, entered the created manifest file, or by selecting the list file by the "Browse" button next to it. After entering the inventory file, click Finish, Eclipse begins to package the project.
Figure 7.4 Inventory file settings
After four steps, a file named "myswt.jar" is generated under the "D: / MySwt_Application" path. MySwt.jar is a zip format compressed file, which can be opened with WinRAR or WinZip software, which means that the two software can also replace the Eclipse wizard to package files. If you pack file with WinRAR, the compression format is selected to select the zip format instead of the RAR format, and the compression ratio can be arbitrarily selected.
After opening the myswt.jar file with WinRAR, its internal directory structure is shown in Figure 7.5:
Figure 7.5 Internal Directory Structure of MySwt.jar file
Only one file in the internal directory Meta-INF of the MySwt.jar file: manifest.mf, it is the same as the content of the manifest.txt created in the previous creation, 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 the support package and localization of the Java application
The class-path item in the manifest.mf file sets three packages, copy this three support packages from the plugins directory of Eclipse to the D: / MySwt_Application / Lib directory, localized file SWT-WIN32-3063.DLL replication In the D: / MySWT_Application directory. This three files in Eclipse are:
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 copy is shown in Figure 7.6: Figure 7.6 Directory Structure
4. Write a batch program "run.bat" running a myswt.jar package
Create a batch program run.bat in mySwt_Application directory (name, extension must be bat), its content is only a statement, as follows:
Javaw -jar myswt.jar
Description:
l JavaW corresponds to the C: /JDK/JRE/bin/javaw.exe file, if the Windows prompt command does not find, you need to add the C: / JDK / JRE / BIN path to the Windows environment variable PATH.
l There is an annoying black command line window when running the program, you can change it, you can change the run.bat content as follows: "Start Javaw -jar MySwt.jar", Start refers to the "Run" command called Windows.
l If you want to put SWT-Win32-3063.dll in a separate directory, such as "D: / MySwt_Application / Native" directory, you need to change 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 renderings
6, precautions
This example only requires three support packages, but your program may need more support packages to run. If you want to in place, you can copy all referenced in the "Java Build Path" tab to the lib directory. If you like to use a package to join what packages, I hope to maintain the simple packaging file, you need to go to step by step: If a support package is missing, the error message that does not find the class when running the program, from the information The package name can know which support package is missing. For example, "Exception in Thread" Main "java.lang.noclassdeffounderror: org / eclipse / jface / wizard / iWizard", you can know the lack of JFACE package from the error message
7.1.3 Other ways to get JAR package
To get the JAR package, the above-described Eclipse "Export" wizard is used, using Winzip and WinRar, and also use the Java self-contained command line package software JAR.exe (located in a C: / JDK / BIN directory), 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 - Since this book does not add C: / JDK / BIN to the Windows environment variable PATH, manually specify the path to jar.exe
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 a list file. Pay attention to them are lowercase
l Myswt.jar - the name of JAR package after packaging
l On the list of manifest.txt in front of the list file in the C: / ECLIPSE3.0.1/eclipse/workspace/myswt/ directory. If you put it and batch files in a directory, you don't have to specify a long path.
l "-c path." Refers to pack all the files of the path (including subdirectory). Since the Class file is output in the bin directory of the project, the path is specified to the bin directory of the project, pay attention to the space between the three Separate, and the last character is a decimal point.
The advantage of this approach is that there is no cumbersome operation of the Eclipse export wizard, suitable for the case where the JAR package is often required.
7.1.4 Packaging the project using a third-party plugin
One of the open source organization (http://sourceforge.net/) can play the Eclipse support package and the project compilation file together in a package, called "fat jar", its download address is "http: // fjp . SourceForge.Net / ", specific downloads no longer explain, install steps to see Chapter 1 SWT Designer installation.
The following steps of FAT JAR are as follows:
(1) Right-click on the project name of the MySWT project, and a "Build Fat Jar" in the menu, as shown in Figure 7.8 below, select the "Build Fat JAR" item.
Figure 7.8 Right-click menu
(2) In the dialog shown in Figure 7.9, the "jar-name" item fills the output path of the JAR package. File list "Manifest" item does not have to fill, the default will automatically create one. "Main-class" item fills in the entry class of the program. Others accept the default, click Next.
Figure 7.9 Output configuration
(3) As shown in Figure 7.10 below, the support package used by the MySWT project will be listed in the window. We only check the three in the picture in Runtime.jar, Swt.jar, JFace.jar, of course, all taste, but the last JAR package will be larger, because Fat Jar will packet all support packages Merge in a JAR package.
Figure 7.10 Select the file to be packaged
After clicking the "Complete" button of Figure 7.10, the jar package myswt.jar will output to the D: / MySwt_ApplicationH directory. As before, to run this JAR package require a batch file and localized file SWT-WIN32-3063.DLL, the only difference is no longer needed to support packets, and its directory structure is shown in Figure 7.11:
Figure 7.11 Directory Structure
Why don't you need an Eclipse support package? That is because the support package is already in the MySwt.jar file, you can see SWT.jar or the like from below to see Swt.jar, and is included in the MySwt.jar package.
Figure 7.12 Internal directory structure of myswt.jar
The contents of the MANIFEST.MF files in the meta-INF directory can be found to be discovered and previously: Class-Path items are not.
Manifest-Version: 1.0
Created-by: Fat Jar Eclipse Plug-in
Main-class: book.chapter_4.wizard_dialog.wizardDialog1
7.1.4 Make User Computers Don't have to install the JRE environment
Usually running the Java program has a prerequisite: User Computer must install the JRE environment first. Although the JRE environment is very simple, it is more step all, it is a little bit of defect. Here is a method that does not have to allow users to install the JRE environment, which acts 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 other names).
(2) Unload JDK and JRE from the unit, which means that the unit does not have a Java operating environment.
(3) Modify the command in the batch file run.bat is "start java1.4.2 / jre / bin / javaw -jar myswt.jar", just before JavaW, add a corresponding path.
Double-click Run.bat to run this Java application without installing a computer that doesn't have a JRE environment.
7.1.5 Further improvement
1, abandon the batch file (* .bat)
Running the program with batch files seems not professional, although it is enough to complete the operating task. But habits are hard to get rid of its influence in dyeing, and people under Windows are used to running the extension is Exe, with * .bat they will feel awkward.
We can use a free applet called Javaalauncher instead of batch files to run the Java program. Javalauncher's download URL is:
http://www.rolemaker.dk/nonrolemaker/javalauncher/marner_java_launcher.htm
Download the file is a compressed package of javalauncher.zip, and the decompressed directory structure is shown in Figure 7.13:
Figure 7.13 javalauncher.zip directory structure
In the directory of the above figure
l Source Directory contains Javaalauncher's source program, written in C
l Changes.txt is a new version of the modification
l Launch.exe is the main program
l Launcher.cfg is a configuration file
l Readme.txt is some explanations and examples
We only need Launch.exe, Launcher.cfg two files, copy the two files to the directory where the package file is located. Launcher.cfg is a text file that only three lines of content, modify it as follows:
.
./java1.4.2/jre/bin/javaw.exe
-jar myswt.jar
l The first line setting points to the directory of JAR package myswt.jar, because Launch.exe and MySwt.jar are in a directory, so "." is the current directory.
l The second line sets the path to JRE / BIN / JAVAW.EXE. In the previous section (sections 7.1.4), the JRE directory has been copied to the java1.4.2 subdirectory.
After configuring launcher.cfg, double-click Launch.exe to run Java applications.
If you study the start-up mode of Eclipse, the principle of Eclipse and Javaalauncher is found: Eclipse.exe is equivalent to Launch.exe, Startup.jar is equivalent to MySwt.jar. It is not even if Eclipse.exe is unimactful, so it does not * .cfg configuration files, but the startup information is cured in Eclipse.exe.
2, beautify icon
The icon of the Launch.exe file is too monotonous, let us change it. The icon for the replacement requires a free software: Resource Hacker, it has a Chinese version, the download URL is:
http://www.users.on.net/johnson/resourcehacker/
The steps to replace the icon of Launch.exe with Resource Hacker are as follows:
(1) Running the resource Hacker to get the window shown in Figure 7.14 below.
Figure 7.14 Main interface of Resource Hacker
(2) Click Main Menu "File → Open" to load Launch.exe into the program, as shown in Figure 7.15 below.
Figure 7.15 Loading the interface (3) after lanunch.exe, as shown above, select the "icon → 1 → 1030" on the left, then right-click "1030" item, select "Replace Resources ...". 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: Icon files can be EXE, DLL, RES, ICO, which can extract icons from EXE, DLL, RES, and this example is a icon file JAVACup.ico for Java.
Figure 7.16 Select the icon file
(4) As shown in Figure 7.17 below, select "File → Save As", name myswt.exe.
Note: According to the reason, select "Save" is also possible. At this time, Resource Hacker backs up the old launch.exe to launch_original.exe. But maybe it is a problem with the problem, and use "save" mode sometimes launch.exe can't display new icons, but sometimes it can.
Figure 7.17 Save the modification
(5) The final directory is shown in Figure 7.18, which can be seen that ISWT.exe (that is, Launch.exe has changed a name) to the Java icon. Double-click MySWT.exe to run the Java application.
Figure 7.18 Last effect
3, the last package
Before sending it to the user, you usually use Winzip or WinRAR to make all the files all the files into a compressed package, and then the user will decompress the compression, the program can run the program, and the Eclipse software is this way.
Another way is to create a single setup.exe file with InstallShield, Installanywhere, which has a single setup.exe file, which has a wizard installation interface, but also inserts a menu item to Windows, for these installer production. For details, please refer to the relevant books.