您的位置:首页 > 理论基础 > 计算机网络

Java Network Launching Protocol (JNLP,java网络加载协议)

2012-08-23 09:31 621 查看
一、jnlp是什么?是java提供的一种让你可以通过浏览器直接执行java应用程序的途径,它使你可以直接通过一个网页上的url连接打开一个java应用程序.好处就不用说了,如果你的java应用程序以jnlp
的方式发布,如果版本升级后,不需要再向所有用户发布版本,只需要更新服务器的版本,这就相当于让java应用程序有了web应用的优点了.

二、jnlp文件描述如下:

1.	<?xml version="1.0" encoding="UTF-8"?>
2.	  <!--codebase 属性指出搜索应用程序资源的顶级URL,下面的icon/jar元素都是以这个URL为基本.-->
3.	  <<SPAN class=hilite1>jnlp</SPAN> codebase="http://127.0.0.1:8081/webstart">
4.	    <information>
5.	    <!-- 在"开始"-"运行"菜单中输入"javaws"或"javaws -viewer"启动Web Start,会看到客户端已经安装的webstart应用程序-->
6.	        <!--title :应用程序标题 vendor:供应商
7.	        title/vendor 元素必须,会显示在用"javaws -viewer"命令
8.	        打开的应用程序缓存查看器(Java Application Cache Viewer)中-->
9.	        <title>HelloWorld</title>
10.	        <vendor>Lively Corporation</vendor>
11.	        <description>HelloWorld Test Example for WebStart.</description>
12.	        <!--homepage :存放有关应用程序的相关文档的URL,如help文件等,仅仅是description作用-->
13.	        <homepage href="http://127.0.0.1:8081/webstart/index.html"/>
14.	        <!--icon 指定图标会显示在应用程序缓存查看器中,
15.	        在查看器中新建webstart快捷方式到桌面时也会显示为快捷方式图标,
16.	        只支持GIF/JPEG格式,其它格式无效-->
17.	        <icon href="./images/logo.jpg"/>
18.	        <!--splash 在sun的文档中提到会出现在webstart启动时的闪屏中,不过可能由于速度问题,我没有观察到-->
19.	        <icon kind="splash" href="./images/logo.jpg"/>
20.	         <!-- 允许离线启动,可以使用javaws -offline命令-->
21.	        <offline-allowed/> 需要什么来搜一搜吧so.bitsCN.com
22.	    </information>
23.	    <resources>
24.	        <!-- 指定客户端需要安装的j2se版本,下面指定为1.5+,
25.	        如果版本是1.4,在链接此<SPAN class=hilite1>jnlp</SPAN>文件时会提示更新j2se版本-->
26.	        <j2se version="1.5+"/>
27.	        <!-- 指定要下载到本地的jar文件(注意,所有的文件都需要打包才能够下载),
28.	        可以包含一些资源文件,如icons/configuration files,可以使用getResource方法取得-->
29.	        <jar href="./jar/jws/helloworld.jar"/>
30.	    </resources>
31.	    <!--application-desc 必须,指定webstart启动时执行jar文件中的哪个类-->
32.	    <application-desc main-class="jws.HelloWorld"/>
33.	</<SPAN class=hilite1>jnlp</SPAN>>

三、应用JNLP部署应用 

(1)编写相关应用,打包成一系列jar; 

(2)根据jnlp文件说明修改相应的jnlp内容,修改相应的url和jar; 

(3)部署jnlp文件和jar到相应的web容器; 

(4)编写访问网页,即可使用该应用〔亦可下载jnlp文件使用jws运行应用〕; 

四、补充 

如果jar需要访问本地文件资源,需要为你的jar文件签名 

先在命令行用keytool产生一个keystore文件. 

keytool -genkey -keystore you.keystore –alias youApp 

命令行用jarsigner签名,当然这时候需要生成keystore时的密码 

jarsigner -keystore you.keystore appTest.jar youApp 

重新部署即可 

实例 

用JavaWebStart运行SWT应用 

SWT:http://www.eclipse.org/swt 

JNLP:http://www.jcp.org/en/jsr/detail?id=56 

JAVA-WebStart:http://java.sun.com/products/javawebstart/index.jsp 

SWT的优势是速度快,适合做应用程序。 

用java webstart启动一个swt应用,是较好的Rich Client方案之一。 

SWT以java-webstart形式运行,可参见: 
http://www-900.ibm.com/developerWorks/cn/linux/opensource/os-jws/index.shtml 
其中关键步骤是对*.jar文件的签名。参见: 
http://www.pconline.com.cn/pcedu/empolder/gj/java/0410/480482_4.html 
SWT Code: 

public class HelloWorld
{
private org.eclipse.swt.widgets.Shell sShell = null;
private Label label1 = null;
private Button button = null;
public static void main(String[] args)
{
org.eclipse.swt.widgets.Display display = org.eclipse.swt.widgets.Display.getDefault();
HelloWorld thisClass = new HelloWorld();
thisClass.createSShell() ;
thisClass.sShell.open();
while (!thisClass.sShell.isDisposed())
{
if (!display.readAndDispatch()) display.sleep ();
}
display.dispose();
}

private void createSShell()
{
sShell = new org.eclipse.swt.widgets.Shell();
label1 = new Label(sShell, SWT.NONE);
button = new Button(sShell, SWT.NONE);
sShell.setSize(new org.eclipse.swt.graphics.Point(300, 122));
sShell.setText("JNLP测试");
label1.setBounds(new org.eclipse.swt.graphics.Rectangle(22, 16, 260, 34));
label1.setText("JNLP Client Run Successful!");
button.setBounds(new org.eclipse.swt.graphics.Rectangle(148, 59, 132, 23));
button.setText("EXIT");
button.addMouseListener(new org.eclipse.swt.events.MouseAdapter()
{
public void mouseUp(org.eclipse.swt.events.MouseEvent e)
{
sShell.close();
}
});
}
}


主要步骤如下: 

1.     将%ECLIPSE_HOME%\plugins\org.eclipse.swt.win32_3.1.0\os\win32\x86目录下的几个dll文件。将其打包为swt-lib-source.jar,并将其复制到临时文件夹。 

2.     将%ECLIPSE_HOME%\plugins\org.eclipse.swt.win32_3.1.0\ws\win32目录下的swt.jar复制到临时文件夹。 

3.     将Swt应用程序client端的*.class以及resource打包,例如命名为jnlp-demo-source.jar 

4.     在临时文件夹生成签名文件: 

keytool -genkey -alias icecloud -keypass password -keystore icecloudstore 

5.     用签名文件对jar文件签名 

jarsigner -keystore mystore -signedjar swt-lib.jar swt-lib-source.jar icecloud 

jarsigner -keystore mystore -signedjar swt-win32.jar swt.jar icecloud 

jarsigner -keystore mystore -signedjar jnlp-demo.jar jnlp-demo-source.jar icecloud 

6.     将生成的jar文件复制到webapp的codebase中,例如直接放在根目录下或者/codebase 

7.     制作jnlp文件,参见ibm教程中的例子 

<?xml version="1.0" encoding="gbk"?>
<jnlp spec="1.0+" codebase="http://icecloud/jnlp" href="index.jnlp">
<information>
<title>HelloWorld</title>
<vendor>ESBU Group</vendor>
<homepage href="index.html" />
<description>Hello World test</description>
<description kind="short">Hello world</description>
</information>
<security>
<all-permissions />
</security>
<resources>
<j2se version="1.5" />
<jar href="jnlp-demo.jar" />
<nativelib href="swt-lib.jar" />
</resources>
<resources os="Windows">
<jar href="swt-win32.jar" />
</resources>
<application-desc main-class="com.zarva.test.jnlp.HelloWorld" />
</jnlp>


8.     ie运行 http://icecloud/jnlp/ 
说明: 

jnlp严格要求j2se的版本来运行,我本机安装的1.5,开始时候使用参数1.4,ie开始自动从sun下载jre1.4 

nativelib可以对jni的library进行处理。因此,不需要将*.lib放入客户机的System32 
签名默认六个月有效。目前还没不知道对系统产生的影响和解决方案。 

jFreeChart 的demo采用此协议,可以去官网看看。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java network 网络 j2se jar swt