Packaging and release of eclipse SWT application projects

Source: Internet
Author: User
Tags export class
After the project is completed, it is packaged and released. The packaging of the application project and Eclipse plug-in Project (plugin) is different. This chapter introduces the packaging methods of the two, the actual packaging example is provided.

7.1 application project packaging and release 7.1.1 Introduction

After the Java application project is completed, it can be run out of eclipse. To run the program, you need to compress it into a jar package, most of its packaging methods are the same as the standard Java AWT/swing packaging methods. The main points are as follows:

L manifest. mf-packing list. It is a key file for packaging. It mainly sets the execution entry class and the path of the supported libraries, when running a Java application, you need to find the entry class and support library based on the information provided in this file.

L support package-if the Java application uses some eclipse packages, you must copy these packages to the program running directory. Otherwise, the program will not run. For example, the SWT component supports the SWT. jar package and the jface component supports the jface. jar package. These packages must be set in the manifest. MF file.

L localization file-If SWT components are used, you also need to copy the SWT localization file swt-win32-3063.dll (3063 is the version number) to the program running directory, otherwise the program will not run.

7.1.2 procedure

This section uses the SWT/jface project "myswt" developed in the previous chapters as an example to describe how to package an application project.

1. Edit manifest. MF.

(1) eclipse provides the "Export" Wizard for packaging projects, but before running this wizard in this example, you need to create a manifest. MF configuration file, the content of which 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

Note:

L manifest-version-specifies the version number of the inventory file

L main-class-specifies the entry class for running the program. This example is used to run the wizard dialog box developed in section 4.5.2. Note: Do not add the class extension after the class name

L Class-path-specifies the path of the supported libraries. "." Indicates the program running directory, that is, the directory where the exported jar package is located. When running the program, you can find the supported libraries based on the set path of the Class-path item. Each supported library is separated by a space. Here, the runtime. jar package is required for jface. jar, so the runtime. jar package must also be added to class-path.

L in addition to the package name and Class Name of the entry class, other settings are case-insensitive. For example, you can write class-path or class-path as the class-path. SWT. jar is written as SWT. jar.

(2) Save the configuration file. We recommend that you store it in the root directory of the myswt project. The corresponding file name can be retrieved at any time. This example is named manifes.txt, And the eclipsewizard automatically copies the content of manifes.txt to the manifest. MF file under the META-INF directory of the jar package.

2. Use the Eclipse "Export" Wizard to package the project

(1) Right-click the project name of the myswt project and choose export from the shortcut menu ". In the displayed dialog box, as shown in 7.1, select "JAR file" and click "Next ".

Figure 7.1 Export Dialog Box

(2) As shown in Figure 7.2, deselect all unnecessary files on the right. In the "select export target" text box, set the output path and package name of the jar package to "D:/myswt_application/myswt. Jar ". Accept other default settings, 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 code file and resource" option is selected.

Figure 7.2 select an import File

(3) as shown in Figure 7.3, accept the default settings and click "Next ".

Figure 7.3 export class options

(4) This step is critical. As shown in Figure 7.4, select "use existing list from workspace", enter the created list file, or click the "Browse" button next to it to select a list file. Enter the configuration file and click "finish". Eclipse starts to package the project.

Figure 7.4 Configuration

After the preceding four steps, a file named "myswt. Jar" is generated in the "D:/myswt_application" path. Myswt. jar is a zip file that can be opened using WinRAR or WinZip software. In other words, these two software can also replace the eclipse Wizard to package files. If you use WinRAR to package the file, You must select the ZIP format instead of the RAR format for the compression format. You can select any compression ratio.

The internal directory structure of myswt. jar file opened with WinRAR is shown in 7.5:

Figure 7.5 internal directory structure of myswt. Jar

In the internal directory META-INF of the myswt. jar file, there is only one file: manifest.mf, which is the same as the contents of the original file manifest.txt, as shown below:

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 packages and local files of Java applications.

In manifest. the class-path item in the MF file sets three packages, copy these three support packages from the eclipse plugins directory to the D:/myswt_application/lib directory, and copy the localization file swt-win32-3063.dll to 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 replication is shown in 7.6:

Figure 7.6 directory structure

4. Compile the batch processing program "Run. Bat" that runs the myswt. jar package"

Create a batch processing program run. bat under the myswt_application directory (the name is required, and the extension must be BAT). The content of this batch is only one statement, as shown below:

Javaw-jar myswt. Jar

Note:

L javaw corresponds to the C:/JDK/JRE/bin/javaw.exe file. If Windows prompts that the command is not found, you need to set C: add the/JDK/JRE/bin path to the Windows environment variable path.

L there is a nasty black command line window when running the program. To remove it, you can run. the bat content is changed as follows: "Start javaw-jar myswt. jar ", start refers to calling the" run "command of windows.

L if you want to put the swt-win32-3063.dll in a separate directory, such as the "D:/myswt_application/native" directory, change the run. Bat content:

Start javaw-djava. Library. Path =./native/-jar myswt. Jar

5. Run the program

Double-click the run. BAT file to obtain the program interface, as shown in Figure 7.7.

Figure 7.7 program running

6. Notes

In this example, only three support packages are required, but your program may need more support packages to run. If you want to put the package in place at one time, you can copy all referenced packages in the "library" tab of "Java build path" to the lib directory. If you want to maintain the simplicity of the package file, you need to try it step by step: If a support package is missing, when the program is running, an error message of the class not found will be output. You can find out which support package is missing from the package name in the information. For example, "exception in thread" Main "Java. Lang. noclassdeffounderror: ORG/Eclipse/jface/Wizard/iwizard", you can see from the error message that the program lacks the jface package.

7.1.3 other methods for obtaining jar packages

To obtain the jar package, in addition to using Eclipse "Export" to export data using winzipand winrar, you can also use Java's own command line packaging software jar.exe (located in the C:/JDK/bin directory). The packaging command is:

C:/JDK/bin/jar cvfm myswt. Jar C:/eclipse3.0.1/Eclipse/workspace/myswt/manifest.txt-C:/eclipse3.0.1/Eclipse/workspace/myswt/bin.

Note:

L c:/JDK/bin/jar-the path of jar.exe is specified manually because C:/JDK/Bind is not added to the Windows environment variable path.

L cvfm-jar.exe parameter, "C" creates a new jar package; "v" prints debugging information on the screen; "F" specifies the generated JAR file name; "M" uses the inventory file. Note that they are all in lower case

L myswt. jar-name of the packaged jar package

L put the inventory file manifest.txt in the C:/eclipse3.0.1/Eclipse/workspace/myswt/directory. If you put it and the batch file in a directory, you don't have to specify a long path.

L "-C path. "package all files in the path (including subdirectories). Because the class file is output to the bin directory of the project, the path is specified to the bin directory of the project, note that the three are separated by spaces and the last character is the decimal point.

The advantage of this method is that it is not as troublesome as the eclipse export wizard, and is suitable for situations where jar packages need to be exported frequently.

7.1.4 package a project using a third-party plug-in

Open source organization (http://sourceforge.net/) has a plug-in that can call eclipse support package and project compilation file together into a package, called "Fat jar", it is "http://fjep.sourceforge.net /", for more information about how to install SWT designer, see Chapter 1st.

The steps for using fat jar are as follows:

(1) Right-click the project name of the myswt project. A "build fat jar" item is displayed in the menu, as shown in Figure 7.8. Select the "build fat jar" item.

Figure 7.8 right-click menu

(2) In the dialog box shown in 7.9, set "jar-name" to the output path of the jar package. You do not need to fill in the "Manifest" item in the file list. By default, one is automatically created. Enter the main-class entry class of the program. Accept the default values for others, and click "Next ".

Figure 7.9 output configuration

(3) as shown in Figure 7.10, the support packages used by the myswt project are listed in the window. We only select the runtime in the figure. jar, SWT. jar, jface. the jar package is larger, because the fat jar package combines all the supported packages into a jar package.

Figure 7.10 select the file to be packaged

After you click the "finish" button in Figure 7.10, the jar package myswt. jar will be output to the D:/myswt_applicationh directory. As before, to run this jar package requires a batch file and a localized file swt-win32-3063.dll, the only difference is that the eclipse support package is no longer needed, and its directory structure is shown in 7.11:

Figure 7.11 directory structure

Why don't I need the eclipse support package? That is because the support package is already in the myswt. jar file. From 7.12, we can see that SWT. jar and so on are all split into directories and included in the myswt. jar package.

Figure 7.12 internal directory structure of myswt. Jar

The manifest. MF file in the META-INF directory contains the following content, which is different from the previous one: the class-path entry is gone.

Manifest-version: 1.0

Created-by: fat jar Eclipse plug-in

Main-class: Book. chapter_4.wizard_dialog.wizarddialog1

7.1.4 Prevent Users From Installing the JRE environment on their computers

There is usually a prerequisite for running a Java program: you must first install the JRE environment on your computer. Although installing the JRE environment is very simple, it is a little bit flawed after all. 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:/myswt_application/java1.4.2" Directory (java1.4.2 can also be changed to another name ).

(2) uninstall JDK and JRE from the local machine, which means that the Java Runtime Environment is not installed on the local machine.

(3) modify the command in the batch file run. BAT to "Start java1.4.2/JRE/bin/javaw-jar myswt. Jar", and add a corresponding path before javaw.

Double-click Run. BAT to run the Java application on a computer without the JRE environment installed.

7.1.5 further improvement

1. Discard the batch processing file (*. BAT)

Running a program with a batch file does not seem professional enough, although it is sufficient to complete the running task. But the habit is like a kind of poison. Once infected, it is difficult to get rid of its influence. In Windows, people are used to running programs with an extension of exe *. bat, they will feel awkward.

We can use a free applet named javalauncher to run Java programs instead of batch files. The download URL of javalauncher is:

Http://www.rolemaker.dk/nonRoleMaker/javalauncher/marner_java_launcher.htm

The downloaded file is a file named javalauncher.zip. The decompressed directory structure is shown in 7.13:

Figure 7.13 javalauncher.zip directory structure

In the Directory

L The Source directory contains the javalauncher source program, which is written in C language.

L changes.txt is a new version modification description

L launch.exe is the main program

L launcher. cfg is the configuration file

L readme.txt is a description and example

We only copy the two files launch.exe and launcher. cfg to the directory where the package is located. Launcher. cfg is a text file with only three lines of content. modify it as follows:

.

./Java1.4.2/JRE/bin/javaw.exe

-Jar myswt. Jar

L The first row sets the directory of jarpackage myswt.jar, because launch.exe and myswt. jar are in the same directory, so "." is used as the current directory.

L set the path to JRE/bin/javaw.exe in the second row. In the previous section (7.1.4), the JRE directory has been copied to the java1.4.2 subdirectory.

After configuring launcher.htm, double-click launch.exe to run the Java application.

For example, if the gozi worker studies the eclipse startup method, it is found that eclipse and javalauncher share the same principle: eclipse.exeis equivalent to launch.exe, and startup. jar is equivalent to myswt. jar. Only eclipse.exeis not as universal as launch.exe, so it does not have a configuration file like *. cfg, And the startup information is fixed in eclipse.exe.

2. Beautify icons

The icons in the launch.exe file are too monotonous. Let's give it a better look. A free software is required for the program change icon: Resource hacker, which has a Chinese version. The download URL is:

Http://www.users.on.net/johnson/resourcehacker/

Use resource hackerto Replace the launch.exe icon as follows:

(1) run resource hacker to obtain the window shown in Figure 7.14.

Figure 7.14 main interface of resource hacker

(2) Click "File> open" on the main menu to load launch.exe to the program. The result is shown in 7.15.

Figure 7.15 interface after lanunch.exe is loaded

(3) For example, select "icon → 1 → 1030" on the left, right-click "1030", and select "replace resource ...". As shown in figure 7.16, click "open new icon file" in the pop-up window, select a satisfactory icon, and click "replace.

Note: The icon files can be EXE, DLL, res, and ico. The software extracts icons from EXE, DLL, and res. In this example, a Java icon file javacup is selected. ICO.

Figure 7.16 select an icon file

(4) As shown in Figure 7.17, select "File> Save as" and name myswt.exe.

Note: You can also select "save". At this time, resource hackerwill convert the old launch.exeinto launch_original.exe. However, there may be a problem with refreshing. When you use the "save" formula, launch.exe cannot display a new icon, but sometimes it can.

Figure 7.17 save changes

(The last directory in the 5th folder is shown in 7.18. the icon in the "myswt.exe(also changed the name of launch.exe) is replaced with the Java icon. Double-click myswt.exe to run the Java application.

Figure 7.18 final effect

3. Final Packaging

Before sending the package to a user, we usually use WinZip or WinRAR to compress all the files into a compressed package. After you get the compressed package, decompress the package and run the program. This is the method of Eclipse.

The other method is to create a single setup.exe file using the installation software installshield1_installanywhere. it has a wizard-based installation interface and can also Insert menu items to the program bar of Windows. For more information about how to use these software, see related books.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.