Java Web Start入門教程

來源:互聯網
上載者:User
著作權:(xiaodaoxiaodao)藍小刀 xiaodaoxiaodao@gmail.com

http://www.blogjava.net/xiaodaoxiaodao/articles/103469.html

轉載請註明來源/作者

關於Web Start的JNLP Specification可以到sun網站http://java.sun.com/products/javawebstart/download-spec.html下載。

下面是用最簡單的HelloWorld方法實現一個Java Web Start的例子,工程webstart目錄結構如下:

目錄說明如下:

images :存放*.jnlp檔案中定義的表徵圖檔案

jar :存放classes打包後的jar檔案

jnlp :存放*.jnlp描述檔案

src :java源碼

WEB-INF/classes :java編譯後的class檔案

webatart 建立步驟如下:

1 .在webstart/src/jws下建立一HelloWorld.java檔案:

package jws;

import javax.swing.*;

public class HelloWorld {

private JFrame jFrame;

private JPanel jPanel;

private JLabel label;

public HelloWorld(){

jFrame = new JFrame("HelloWorld Test Frame");

label = new JLabel("Hello, world!");

jPanel = new JPanel();

jPanel.add(label);

jFrame.add(jPanel);

jFrame.pack();

jFrame.setSize(400, 150);

jFrame.setLocation(400, 300);

jFrame.setVisible(true);

jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

// webstart 啟動時執行的主方法

public static void main(String args[]){

new HelloWorld();

}

}

註: 在main方法中完全可以不用寫new HelloWorld();,而只在main方法中輸出一條測試語句

System.out.println("This is test!");

進行簡單測試,列印結果會出現在步驟7java控制台中

2 .編譯HelloWorld.java,在WEB-INF/classes目錄下執行命令:

jar cvf helloworld.jar jws/*.class

執行後把產生的helloworld.jar包拷貝到webstart/jar/jws目錄下。

(在IDEA中打包使用Jar Tool外掛程式比較方便,eclipse中可以使用Fatjar)

3 .從helloworld.jar/META-INF中解壓出MNIFEST.MF,更改MNIFEST.MF內容:

Manifest-Version: 1.0

Created-By: 1.5.0_01 (Sun Microsystems Inc.)

為:

Manifest-Version: 1.0

Created-By: 1.5.0_01 (Sun Microsystems Inc.)

Main-Class: jws.HelloWorld

然後在WEB-INF/classes目錄下執行命令:

jar umf MANIFEST.MF helloworld.jar

把新的MNIFEST.MF更新到helloworld.jar包中,

註: 如果在下面步驟4中的<application-desc main-class="jws.HelloWorld"/>指定了Main-Class方法,此步驟可以省略。

4 .webstart/jnlp目錄下建立一HelloWorld.jnlp檔案:

<?xml version="1.0" encoding="UTF-8"?>

<!--codebase 屬性指出搜尋應用程式資源的頂級URL,下面的icon/jar元素都是以這個URL為基本.-->

<jnlp codebase="http://127.0.0.1:8081/webstart">

<information>

<!-- 在"開始"-"運行"菜單中輸入"javaws"或"javaws -viewer"啟動Web Start,會看到用戶端已經安裝的webstart應用程式-->

<!--title :應用程式標題 vendor:供應商

title/vendor 元素必須,會顯示在用"javaws -viewer"命令

開啟的應用程式緩衝查看器(Java Application Cache Viewer)中-->

<title>HelloWorld</title>

<vendor>Lively Corporation</vendor>

<description>HelloWorld Test Example for WebStart.</description>

<!--homepage :存放有關應用程式的相關文檔的URL,如help檔案等,僅僅是description作用-->

<homepage href="http://127.0.0.1:8081/webstart/index.html"/>

<!--icon 指定表徵圖會顯示在應用程式緩衝查看器中,

在查看器中建立webstart捷徑到案頭時也會顯示為捷徑表徵圖,

只支援GIF/JPEG格式,其它格式無效-->

<icon href="./images/logo.jpg"/>

<!--splash 在sun的文檔中提到會出現在webstart啟動時的閃屏中,不過可能由於速度問題,我沒有觀察到-->

<icon kind="splash" href="./images/logo.jpg"/>

<!-- 允許離線啟動,可以使用javaws -offline命令-->

<offline-allowed/>

</information>

<resources>

<!-- 指定用戶端需要安裝的j2se版本,下面指定為1.5+,

如果版本是1.4,在連結此jnlp檔案時會提示更新j2se版本-->

<j2se version="1.5+"/>

<!-- 指定要下載到本地的jar檔案(注意,所有的檔案都需要打包才能夠下載),

可以包含一些資源檔,如icons/configuration files,可以使用getResource方法取得-->

<jar href="./jar/jws/helloworld.jar"/>

</resources>

<!--application-desc 必須,指定webstart啟動時執行jar檔案中的哪個類-->

<application-desc main-class="jws.HelloWorld"/>

</jnlp>

註:

其中*.jnlp檔案的文法的詳細資料可以在sun網站上查詢http://java.sun.com/j2se/1.5.0/docs/guide/javaws/developersguide/syntax.html

其中javaws命令列的詳細參數可以在sun網站上查詢http://java.sun.com/j2se/1.5.0/docs/guide/javaws/developersguide/javaws.html#options

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.