Deploy the SWT application using Java Web Start

xiaoxiao2021-03-06  83

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

2

3

4 File Viewer </ Title></p> <p>5 <vendor> IBM DeveloperWorks </ vendor></p> <p>6 <homepage href = "index.html" /> 7 <description> File Viewer </ description></p> <p>8 <Description Kind = "Short"> FileViewer </ Description></p> <p>9 </ information></p> <p>10 <security></p> <p>11 <all-permissions /></p> <p>12 </ security></p> <p>13 <resources></p> <p>14 <J2SE Version = "1.4" /></p> <p>15 <jar href = "fileviewer.jar" /></p> <p>16 <nativeiB href = "swt-lib.jar" /></p> <p>17 </ resources></p> <p>18 <resources os = "windows"></p> <p>19 <jar href = "SWT-WIN32.JAR" /></p> <p>20 </ resources></p> <p>21 <resources os = "linux"></p> <p>22 <jar href = "swt-linux.jar" /></p> <p>23 </ resources></p> <p>24 <Application-Descmain-Class = "Org.eclipse.swt.examples.FileViewer.FileViewer" /></p> <p>25 </ jnlp></p> <p>Let us check some of this important part of this XML file:</p> <p>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.</p> <p>For more information on the various elements and formats of the JNLP file, please refer to the reference below.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-107780.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="107780" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.044</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '5s0Tt6K2drQjdyov7QzWBDmuGUMHc0NcyfWBnORCvORISNhsSrHd0TJJCooNsra_2BNxV83zwy25fcpc3GYJTT0A_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>