在 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 成功启动应用程序,但弹出消息显示:“代码库是:[null]”。
当部署在 Web 服务器中并通过使用“http://”而不是“file://”作为代码库协议(protocol)的 JNLP 文件启动时,相同的代码可以正确检索代码库。
当使用带有“file://”代码库的 JNLP 描述符部署在本地(或共享网络)驱动器上时,是否有方法检索 WebStart 应用程序的代码库?
如果不是,我还能如何以编程方式找到应用程序目录的 URL 以访问资源文件(未打包在 jar 文件中)?
此外,从最佳实践的角度来看:在共享网络驱动器而不是网络服务器中部署 WebStart 应用程序(为了简单起见)是一种合理的方法,还是应该避免的黑客攻击?
谢谢。
最佳答案
您可以尝试从已知类的 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
关于java - 为什么 BasicService.getCodeBase() 在使用文件 ://as URL protocol in the JNLP codebase attribute? 时返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18087638/