SWT Overview It is the most appropriate to introduce the standard widget toolbox (SWT) to the development community. Although SUN's Java Foundation (JFC), the Sun's Java Foundation Class, which has always made the client application developers satisfied, but there are still many developers who are not willing to use SWT for a variety of reasons. For many developers and architect designers, you can develop a visualized manner as a traditional native desktop app that shows, operations, and executive client-side Java applications that seem far away.
However, in the middle of 2001, the Eclipse project suddenly appeared in this development of "vision". Initially, people think that the Eclipse project is just a Java open source integration development environment (IDE); however, the project involves wider range. During the development of Eclipse, the SWT toolbox was born. The SWT toolbox is similar to JFC and its "Cang Brothers" abstract window toolbox (AWT), which provides a group of widgets to developers. However, the main difference between SWT and other toolbox is that SWT uses the underlying GUI widget of the operating system. This approach provides Java developers with cross-platform APIs to implement "appearance" solutions to this desktop application.
Java Web Start Overview If these suitable tools, the deployment and maintenance of traditional desktop applications may be the main headache for developers regardless of these suitable tools. Even the most effective support department, I must check each workstation when I want to release the new version of the software, and I can't let them feel a headache. In order to facilitate the distribution and maintenance of Java developers, Sun Microsystem has introduced Java Web Start a few years ago. This technology greatly simplifies the deployment, maintenance, and updates of the Java application on the end user workstation.
Using a web browser as the initial tool that launches the application, Java Web Start is a help app installed locally, which is included in the Sun's Java runtime environment. Whenever the user accesses the Java Network Launch Protocol, JNLP file, the application is automatically downloaded to the user's workstation, then the application starts running. The entire process does not need to interact with end users, which greatly simplifies the work of distributing applications. Please refer to the reference information at the end of this article for more information on Java Web Start.
File Viewer Application Before we learn how to use Java Web Start to deploy SWT applications, let's take a look at the application that will deploy. This application included in this article is an example application provided by the Eclipse project. As shown in Figure 1, the application is a simple cross-platform file viewer that allows users to browse files and folders on their hard drives.
Figure 1. File viewer running on Windows XP
To demonstrate the cross-platform capabilities of SWT and Java Web Start, this independent sample application has been packaged to run on Microsoft Windows and Red Hat Linux.
Environmental requirements can download the code used in this document at the end of this article. In order to completely test this file viewer application with Java Web Start, your environment must meet the following minimum requirements (see the download link in the reference):
Microsoft Windows XP or Red Hat Linux 8 operating system installed Java 2 SDK Standard Edition 1.4 or higher installed Apache Ant 1.5.3 or higher installed Jakarta Tomcat 4.1.24 or higher Although you always build Ant build The script can be used across platforms, but we only test and verify file viewer applications on Microsoft Windows XP and Red Hat Linux 8 operating systems. In addition, it is assumed that Tomcat is running on your local workstation and runs on port 8080. Let us start from the installation, build and run the sample application.
Installing the file viewer application To install a file viewer application, complete the following steps:
Download the source code package by links in the reference materials. Unzip the FileViewer.zip file to the temporary directory.
Different security and code signatures are different from AWT and JFC, and the SWT is closely integrated with the operating system's native window environment. SWT uses system libraries to manage your integration between your own API and the underlying operating system. By default, the application running under Java Web Start is running in a restricted sandbox. This environment controls access to resources (such as file systems, system libraries, and other resources). Since SWT needs to be loaded into the system library outside of the default sandbox, all JAR files deployed with Java Web Start must pass a digital signature. Because we are not preparing to deploy the application to the vast number, a self-signed test certificate is created.
If you are ready to put an SWT application in a production environment, then we strongly recommend that you get a certificate from the certification center (such as Thawte or VeriSign).
Creating a self-signed certificate To create a self-signed test certificate, complete the following steps:
Perform the following command in the directory of the source code decompression: KeyTool -Genkey -Keystore KeyStore -Alaias Myself. The system will prompt you to provide details on new keys, including passwords, names, and location. To use the available ANT build procedure, make sure the password is set to "Password". If you don't want to use the hard-coded password "password", edit the "Sign-Jars" target file in the build.xml file. The actual process of the signature each JAR file is completed during the ANT built. To verify that the keystore is created correctly, perform the following command in the directory that decompress the source code: keytool -list-keystore KeyStore output results display:
KeyStore Type: JKS
KeyStore Provider: Sun
Your KeyStore Contains 1 Entry
Myself, Jun 3, 2003, Keyentry,
Certificate FingerPrint (MD5): B8: C4: 48: ED: 68: E8: Fe: 47: 83: 78: F5: 14: 4E: 28: 5C: 80
Building a File Viewer App To build a file viewer application, complete the following steps:
In order to clean the environment, perform the following command in the directory of the source code decompression: Ant Clean executes the following command to start the build process: Ant
If your environment meets the needs and configures correct, then you should see the following:
Buildfile: build.xml
INIT:
Compile-Common:
Compile-module:
[echo] compling ...
Package-Common:
Package-swtlib:
Sign-jars:
[echo] SIGNING JARS ...
[Signjar] SIGNING JAR: D: /FILEVIEWER/dist/swt-lib.jar [Signjar] Signing Jar: D: /fileviewer/dist/fileViewer.jar
[COPY] COPYING 1 File to D: / fileviewer / dist
[Signjar] Signing Jar: D: /FileViewer/dist/swt-win32.jar
Package-web:
[WAR] building war: d: /fileviewer/dist/fileviewer.war
DEFAULT:
Build Successful
Total Time: 6 seconds
Running File Viewer Application Before using the Java Web Start Deployment File Viewer Application, let's try to execute the app locally. In order to start the application, perform the following command in the directory of the source code decompression: ant run
After performing this Ant script, the file viewer application should appear. Figure 2 shows the application running under Red Hat Linux 8.
Figure 2. File viewer running on Red Hat Linux 8
Deployment and Test File Viewer App To deploy the application to Tomcat 4, complete the following steps:
Copy the FileViewer.war file in the Dist directory into the webapps directory under Tomcat 4. Execute this script to start Tomcat 4. Open a web browser and enter this URL, then click the "Launch Application" link: http: // localhost: 8080 / fileviewer / index.html Click the link, you should see the following image (Figure 3) At this time, the application's JAR file will download to the workstation: Figure 3. File Viewer When you download the application, you will see the following dialog, as shown in Figure 4: Figure 4. Security Warning Click "Start" to start the file viewer application.
Realizing JNLP files Since we have learned how to build, distribute and deploy SWT applications with Sun's Java Web Start, let's retrieve how to implement JNLP files. Manage Java Network Starting Protocol Specifications through Java Community Process (JCP). Since this paper is to introduce some of the specific details of the SWT application with Java Web Start, there is no high background information. For more information on Java Web Start, please refer to the article "Java Web Start" listed in the reference information.
Listing 1 illustrates the various tags used in the JNLP file of the SWT application.
Listing 1. Index.jnlp file
1 Xmlversion = "1.0" eNCoding = "UTF-8"?>
2
3
4
5
6
8
9 information>
10
11
12 security>
13
14
15
16
17 resources>
18
19
20 resources>
21
22
23 resources>
24
25 jnlp>
Let us check some of this important part of this XML file:
Because the SWT application needs to load the system library when executed, it needs to fully access the user's workstation. Chain 10 to 12th lines show how the application requests full access to the client environment. In order to grant access to Java Web Start, all JAR files of the application must have a digital signature. As the 16th line of line 16, we need to declare a NativeLib tag to tell Java Web Start this JAR file contains the locale. In our sample file viewer application, this JAR file contains a Windows DLL and a series of SO files for Linux. Each operating system is loaded into the corresponding SWT JAR file per operating system. "Resources" tag allows you to protect resources, thereby only specific operating systems and architectures can use this resource. The OS property specifies the operating system that should be considered using the resource element. This value is based on the end user's Os.Name Java system feature. Although this value is not used in this example, you can also use the Arch property to further limit access to the Java library. The Arch property is based on the end user's Os.arch Java system feature.
For more information on the various elements and formats of the JNLP file, please refer to the reference below.