Eclipse SWT application project package and distribution (transfer) When the project is completed, the package is issued, the application (Application) project and the Plugin package is different, and this chapter will introduce both Package method and give actual packaging examples. 7.1 Application Project Package and Release 7.1.1 Introduction After the Java application project is complete, you can get rid of Eclipse, you want to run the program to be hit into a JAR package, which packs most methods and standard Java's AWT Swings method, mainly have the following points 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. 7.1.2 Details of Package This section will use the SWT / JFACE project "MySwt" package developed by the first few chapters as an example to introduce the method of the package application project. 1. Edit List Manifest.mf (1) Eclipse provides an "Export" Wizard for Package Projects, but you need to create a manifest.mf list file before you run this wizard. The content is as follows: Manifest-Version: 1.0mAin -Class: book.chapter_4.wizard_dialog.wizarddialog1class-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: Do not add a Class extension L class-path - Specify 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 7.1 Export dialog (2) As shown in Figure 7.2 below, the files that are not needed on the right are canceled. 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 7.2 Selecting the import file (3) As shown in Figure 7.3 below, accept the default settings unchanged, click Next. Figure 7.3 Options (4) of the derived class (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 setting After the above 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 shows: Figure 7.5 Myswt.jar file Internal Directory structure in the internal directory of the myswt.jar file, only one file: manifest.mf, it The content of manifest.txt created in the previous creation is the same, as follows: Manifest-Version: 1.0class-path: ./lib/swt.jar ./lib/jface.jar ./lib/runtime.jarmain-class: Book.chapter_4.wizard_dialog.wizardDialog13, copy the Java application support package and localized files set three packages in the manifest.mf file, and copy these three support packages from the Plugins directory of Eclipse to D: / mySwt_Application / lib directory, localized file SWT-WIN32-3063.DLL is copied to the D: / MySwt_Application directory.
The path in Eclipse is: plugins / org.eclipse.swt.win32_3.0.1 / ws / win32 / swt.jrplugins / org.eclipse.jface_3.0.0 / jface.jarplugins / org.eclipse.core.Runtime_3 .0.1 / runtime.jarplugins / org.eclipse.swt.win32_3.0.1 / OS / WIN32 / X86 / SWT-WIN32-3063.DLL replication is shown in Figure 7.6 shows: Figure 7.6 Directory Structure Figure 4, write The batch program "run.bat" running the myswt.jar package "run.bat" creates a batch program run.bat in the MySwt_Application directory (name, the 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 a 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 the "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. Figure 7.7 Program Run Render Figure 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 7.1.3 from the error message, you can get the JAR package. The JAR package has the above use of the Eclipse "Export" wizard, with Winzip and WinRAR, and also use Java's own command line to package software jar.exe, and its packaging command is : C: / JDK / BIN / JAR CVFM MySwt.jar C: /eclipse3.0.1/eclipse/Workspace/Myswt/Manifest.txt -cc: /eclipse3.0.1/eclipse/Workspace/myswt/bin. Description: l C: / JDK / BIN / JAR - Since the book does not add C: / JDK / BIN to the Windows environment variable path, manually specify the path of 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 the list file. Note that they are lowercase l mySwt.jar - the packaged JAR package name L is placed in the list file manifest.txt 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 A third-party plug-in has a plug-in that the project package open source organization (http://sourceforge.net/) can play the eclipse support package and the project compilation file to a plug-in in a package, called "fat jar", it The download address is "http://fjep.sourceforge.net/", the specific download is no longer explained, the installation step See Chapter 1 SWT Designer installation. FAT JAR Usage steps are as follows: (1) Right-click on the project name of the MySWT project, you can see more "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.
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 Click the "Complete" button of Figure 7.10, the jar package myswt.jar will output to the D: / MySwt_ApplicationH directory. As before, run this JAR package to need a batch file and localized file SWT-WIN32-3063.DLL, the only difference is no longer needed to support packets, the 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 where the MANIFEST.MF file content of the meta-inflicity is as follows, and the previously different places can be: Class-Path item is not. Manifest-Version: 1.0created-by: fat jar Eclipse Plug-Inmain-Class: book.chapter_4.wizard_dialog.wizardDialog17.1.4 Let User Computer Do not have to install the JRE environment A prerequisite for running the Java program: User Computer must install the JRE environment . 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 the user to install the JRE environment, which is implemented as follows: (1) Copy the "JRE" directory in the original JDK to "D: /MYSWT_APPLICATION/JAVA1.4.2" directory (Java1.4.2 can also Change to another name). (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 improve 1, abandonment of batch file (* .bat) Use batch file running program 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.
Javaalauncher download URL is: http://www.rolemaker.dk/nonrolemaker/javalauncher/marner_java_launcher/marner_java_launcher.htm Download the file is a compressed package of Javaalauncher.zip, the decompressed directory structure is shown in Figure 7.13: Figure 7.13 The Javaalauncher.zip directory structure is in the directory of the image. The source directory contains Javaalauncher's source. L Changes.txt written with C language is the new version of the modification. L Launch.exe is the main program L Launcher.cfg is a configuration file l Readme.txt is some instructions 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 is only three-line content, modifies it as follows: ../ java1.4.2 / jRE / bin / javaw.exe -jar myswt.jarl The first line setting points to the JAR package myswt.jar directory, Since Launch.exe and MySwt.jar are in a directory, use "." That is, the current directory. l The second line sets the path to JRE / BIN / JAVAW.EXE. After the previous section (Section 7.1.4) has copied the JRE directory to the Java1.4.2 subdirectory, double-click Launch.exe to run the Java application. 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 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/resourcehac/ Use resource Hacker to replace the icon of Launch.exe The steps are as follows: (1) Running the resource Hacker to get the window shown in Figure 7.14 below. Figure 7.14 Resource Hacker's main interface (2) Click Main Menu "File → Open", 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.