访问 JAR 资源 [英] Accessing JAR resources

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

问题描述

我有一个 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');

遗憾的是这行不通.

有什么想法吗?谢谢!

PS:显然我不能使用绝对路径,我想避免如果可能,环境变量

PS: Obviously I cannot use absolute paths, and I'd like to avoid environment variables if possible

推荐答案

确保您的资源文件夹在您的项目设置中列为源文件夹.另外,请确保在构建 jar 时将资源文件夹设置为导出.

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.

您可以将 .zip 扩展名添加到您的 jar 文件,然后打开它以确保您的资源包含在预期位置.

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屋!

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