【原】jnlp 分平台加载jar包 需要注意的配置

在网络上找了很久,没有找到jnlp的完整的shema文档。这里找到部分资料,记录下,关于分平台记载jar包问题。

 

一、配置问题

The Java Web Start Resource tag

The “Resource” tag in the Java Web Start descriptor has two key elements to allow efficient Java application deployment. One important attribute of the resource tag is the “os” attribute. This attribute allows you to specify specific platforms for the deployment of your application. Options in the “os” attribute can be “Windows”, “Linux”, “Mac OS X” etc.

The next relevant resource attribute is the “arch” attribute. This attribute allows for the specification of the processor architecture. Relevant values for the “arch” attribute include “x86″ and “amd64″ (even for Intel 64bit). Additional “arch” values are seen below in the Web Start JNLP example code.

The following is an example of a Web Start JNLP file (“TestBrowserAllPlatforms.jnlp”):

 

<jnlp
spec="1.0+"
codebase="{Set the base URL here where all jars stored. For example http://www.webrenderer.com/webstart/}"
href="TestBrowserAllPlatforms.jnlp">

<security>
<all-permissions/>
</security>
<resources>
<j2se version="1.4+"/>
<jar href="TestBrowser.jar"/>
<jar href="webrenderer-swing.jar"/>
</resources>

<resources os="Windows" arch="amd64">
<jar href="webrenderer-swing-windows64.jar"/>
<jar href="corecomponents-swing-windows64.jar"/>
</resources>

<resources os="Windows" arch="x86">
<jar href="webrenderer-swing-windows32.jar"/>
<jar href="corecomponents-swing-windows32.jar"/>
</resources>

<resources os="Linux" arch="amd64">
<jar href="webrenderer-swing-linux64.jar"/>
<jar href="corecomponents-swing-linux64.jar"/>
</resources>

<resources os="Linux" arch="i386 x86">
<jar href="webrenderer-swing-linux32.jar"/>
<jar href="corecomponents-swing-linux32.jar"/>
</resources>

<resources os="Mac\ OS\ X" arch="x86_64">
<jar href="webrenderer-swing-osx64.jar"/>
<jar href="corecomponents-swing-osx64.jar"/>
</resources>

<resources os="Mac\ OS\ X" arch="i386 x86">
<jar href="webrenderer-swing-osx32.jar"/>
<jar href="corecomponents-swing-osx32.jar"/>
</resources>

<application-desc main-class="TestBrowser"/>
</jnlp>

 

二、动态生成问题:

 

(如果tomcat版本老还得去配置conf/web.xml,加上

<mime-mapping>
     <extension>jnlp</extension>
      <mime-type>application/x-java-jnlp-file</mime-type>
</mime-mapping>)

    代码:

 

PrintWriter out = response.getWriter();
 
    response.setContentType("application/x-java-jnlp-file");
    out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    out.println("<jnlp spec=\"1.0+\" codebase=\"http://localhost\" href=\"report/jnlp/openFile.do\">");
    out.println("<information>");
    out.println("<title>JNLP</title>");
    out.println("<vendor>Hust</vendor>");
    out.println("<homepage href=\"\"/>");
    out.println("<description>Web Start</description>");
    out.println("</information>");
    out.println("<security>");
    out.println("</security>");
    out.println("<resources>");
    out.println("<j2se version=\"1.3+\"/>");
    out.println("<jar href=\"finereport.jar\"/>");
    out.println("</resources>");
    out.println("<application-desc main-class=\"com.vanda.report.ZDesigner\">");
    out.println("</application-desc>");
    out.println("</jnlp>");
out.flush();
out.close();

 

   三、动态生成缓存问题:

在实际应用中,由于JNLP和JWS本身的bug,在某些情况下,后台jar程序更新升级后,用户侧启动jnlp并不能获得更新,需要强行清空JWS缓存才行,这肯定不是一般用户懂得的。还有一种情况,就是由于ERP本身的jar包发生了变化(例如发生了增减),此时相当于jnlp文件的内容发生了变化。这时候,要求用户一侧机器必须意识到jnlp的变化并先将jnlp进行更新。在很多java版本中(例如jre6的早期版本——例如jre6 update20之前),由于潜在的一些bug等原因,都不能顺利地进行更新,导致程序启动失败。

如何解决这一情况呢?采用动态jnlp是一个有效的方法。

动态jnlp的思路是:在服务器的后端,通过jsp或servlet来动态的生成一个jnlp文件,而不是放置一个静态的固定不变的jnlp文件。这样,jnlp文件内容就可以通过后台应用的逻辑进行动态生成创建:需要什么jar包、需要什么jre版本等等。

以jsp为例。在这个jsp中,首先要注意的几个技术点是:要设置本页面不要被浏览器缓存,放置jnlp内容变化无法及时被更新;其次要设置mime类型让浏览器认为它是一个jnlp文件,以便下载执行而不是直接在浏览器中显示出来。通过设置response即可达到这些目的:

response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "0");
response.setHeader("Content-Disposition", "filename=\"bb.jnlp\";");
response.setContentType("application/x-java-jnlp-file");

 

 一个需要注意的问题是,在动态生成jnlp文件时,要注意jnlp文件中的href标签不要进行设置。为什么呢?看一下jnlp的格式文档是这样说的:
http://lopica.sourceforge.net/ref.html#jnlp

The jnlp file's one and only root.

Attributes
spec=version , optional
Specifies what versions of the jnlp spec a jnlp file works with. The default value is 1.0+. Thus, you can typically leave it out.
version=version , optional
Specifies the version of the application as well as the version of the jnlp file itself.
codebase=url , optional
Specifies the codebase for the application. Codebase is also used as base URL for all relative URLs in href attributes.
href=url , optional
Contains the location of the jnlp file as a URL. If you leave out the href attribute, Web Start will disable the update check on your JNLP file, and Web Start will not treat each new JNLP file as an application update – only updated jar files will. Leaving out href usually makes only sense if your jnlp file is created dynamically (that is, throug a cgi-script, for example) and if your jnlp file’s arguments or properties change from request to request (user to user).
Note, that Java Web Start needs href to list your app in the Web Start Application Manager.

可见在动态生成jnlp时候就不要设置href了,这样就可以保证每次浏览器会重新下载jnlp文件内容,否则可能会被缓存,无法及时更新程序。

猜你喜欢

转载自rmzdb.iteye.com/blog/2113321