从类自己的JAR文件加载资源 [英] Load resource from class's own JAR file

查看:144
本文介绍了从类自己的JAR文件加载资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Class.getResource()并提供相当常见的资源名称,例如license.html ,Java可能会从CLASSPATH中前面列出的另一个JAR文件加载license.html资源,但可能与我的应用程序完全无关。有没有办法强制ClassLoader 第一次尝试从我的类所在的同一个JAR文件加载资源,只有在找不到的时候,查看CLASSPATH上的其他JAR?

When using Class.getResource() and supplying a rather common resource name, like "license.html", Java may load a "license.html" resource from another JAR file which is listed earlier in the CLASSPATH, but may be completely unrelated to my application. Is there a way to force the ClassLoader to first try to load the resource from the same JAR file which my class is in and only if not found, look in other JARs on the CLASSPATH?

推荐答案

或者你可以使用 JarURLConnection 如果您知道文件所在的确切jar:

Alternatively you could use the JarURLConnection if you know the exact jar in which your file resides:

jar:<url>!/{entry}

这篇关于从类自己的JAR文件加载资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆