访问JAR资源 [英] Accessing JAR resources
问题描述
我有一个 jar
文件,其中包含我要分发的资源(主要是缓存,日志记录等配置)。
I have a jar
file with resources (mainly configuration for caches, logging, etc) that I want to distribute.
我遇到了这些资源的相对路径问题,所以我做了我在另一个stackoverflow问题中发现的问题,该问题说这是一种有效的方式:
I'm having a problem with the relative paths for those resources, so I did what I've found in another stackoverflow question, which said that this was a valid way:
ClassInTheSamePackageOfTheResource.class.getResourceAsStream('resource.xml');
可悲的是,这不起作用。
Sadly this does not work.
有任何想法吗?谢谢!
PS:显然我不能使用绝对
路径,我想避免
环境变量如果可能
PS: Obviously I cannot use absolute paths, and I'd like to avoid environment variables if possible
推荐答案
确保资源文件夹在项目中列为源文件夹设置。另外,确保在构建jar时将资源文件夹设置为export。
Make sure that your resource folder is listed as a source folder in your project settings. Also, make sure that the resource folder is set to export when you build your jar.
您可以在jar文件中添加.zip扩展名,然后将其打开以确保您的资源包含在预期的位置。
You can add a .zip extension to your jar file then open it up to ensure that your resources are included and at the expected location.
我总是使用这样的绝对路径:
I always use absolute paths like this:
InputStream input = this.getClass().getResourceAsStream("/image.gif");
使用绝对路径时,/是jar文件中的根文件夹,而不是根文件夹主机的文件夹。
When you use absolute paths, "/" is the root folder in the jar file, not the root folder of the host machine.
这篇关于访问JAR资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!