首页 文章

在JNLP代码库属性中使用file://作为URL协议时,为什么BasicService.getCodeBase()返回null?

提问于
浏览
3

在WebStart应用程序中,我使用此代码以编程方式检索JNLP代码库:

package web.start.test;

import java.net.URL;

import javax.jnlp.BasicService;
import javax.jnlp.ServiceManager;
import javax.jnlp.UnavailableServiceException;
import javax.swing.JOptionPane;

public class WebStartTest
{
    public static void main(String[] args)
    {
        try
        {
            BasicService basicService = (BasicService) ServiceManager.lookup("javax.jnlp.BasicService");
            URL codeBase = basicService.getCodeBase();
            JOptionPane.showMessageDialog(null, "Code base is: [" + codeBase + "]");
        }
        catch (UnavailableServiceException exception)
        {
            throw new RuntimeException(exception);
        }
    }
}

我正在使用此JNLP描述符来启动应用程序:

<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+" codebase="file:/d:/playground/eclipse/WebStartTest" href="WebStartTest.jnlp">
    <information>
        <title>WebStartTest</title>
        <vendor>Web Start Tester</vendor>
    </information>
    <security>
        <all-permissions/>
    </security>
    <resources>
        <j2se version="1.7+" />
        <jar href="target/WebStartTest-0.0.1-SNAPSHOT.jar" />
    </resources>
    <application-desc main-class="web.start.test.WebStartTest" />
</jnlp>

打包并签署jar文件后,在JNLP文件上按Enter键会成功启动应用程序,但弹出消息显示: "Code base is: [null]" .

当部署在Web服务器中时,相同的代码会正确检索代码库,并通过使用“http://”而不是“file://”作为代码库协议的JNLP文件启动 .

有没有办法在使用带有“file://”代码库的JNLP描述符部署在本地(或共享网络)驱动器上时检索WebStart应用程序的代码库?

如果没有,我还能以编程方式找到应用程序目录的URL以访问资源文件(未包装在jar文件中)?

此外,从最佳实践的角度来看:将WebStart应用程序部署在共享网络驱动器而不是Web服务器中(为简单起见)是一种合理的方法,还是应该避免的hack?

谢谢 .

3 回答

  • 1

    您可以尝试从已知类的URL派生应用程序代码库 .

    URL url = WebStartTest.class.getResource("/web/start/test/WebStartTest.class");
    
    System.out.println("url = " + url);
    

    版画

    url = file:C:/projects/Test/classes/web/start/test/WebStartTest.class
    
  • -1

    因为,遗憾的是,这是自Java 7 Update 25以来的方式,正如Release Notes中提到的关于 Applet 的......并且这似乎也适用于 BasicService

    本地Applet为代码库返回NULL从JDK 7u25开始,当applet从本地文件系统运行时,applet的getCodeBase()方法将返回NULL .

    不幸的是,Javadoc尚未更新以反映这一点 .

    是的,使用共享驱动的Web Start会让你进入一个痛苦的世界,我会沿着那条路走下去,因为你害怕遇到越来越多的问题 . 并不是说我特别不喜欢Web Start,但在过去的两年中,许多更新引入了非常时髦的错误 .

  • 1

    可能是因为你使用'file:/'而不是'file://'?

相关问题