访问 JAR 资源

发布于 2024-08-29 02:04:54 字数 348 浏览 2 评论 0原文

我有一个 jar 文件,其中包含我想要分发的资源(主要是缓存、日志记录等配置)。

我对这些资源的相对路径有问题,所以我做了我在另一个 stackoverflow 问题中发现的事情,该问题说这是一种有效的方法:

ClassInTheSamePackageOfTheResource.class.getResourceAsStream('resource.xml');

遗憾的是,这不起作用。

有什么想法吗?谢谢!

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

I have a jar file with resources (mainly configuration for caches, logging, etc) that I want to distribute.

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.

Any ideas? Thanks!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独孤求败 2024-09-05 02:04:55

我通常存储文件和其他资源,然后将它们作为 URL 检索:

URL url = MyClass.class.getResource("/design/someResource.png");

在静态上下文中,或者

URL url = getClass().getResource("/design/someResource.png");

从实例中检索。

上面的代码片段假设 design 是 jar 中的顶级文件夹。一般来说,如果路径以“/”开头,则假定为绝对路径,否则它是相对于类位置的路径。

I usually store files and other resources and then retrieve them as URLs:

URL url = MyClass.class.getResource("/design/someResource.png");

Within a static context, or otherwise:

URL url = getClass().getResource("/design/someResource.png");

From an instance.

The above snippets assume that design is a top level folder within the jar. In general, if the path begins with a "/" it assumes an absolute path, otherwise it's relative from the class location.

回眸一遍 2024-09-05 02:04:54

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

您可以向 jar 文件添加 .zip 扩展名,然后将其打开,以确保您的资源包含在内并位于预期位置。

我总是这样使用绝对路径:

InputStream input = this.getClass().getResourceAsStream("/image.gif");

当使用绝对路径时,“/”是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.

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");

When you use absolute paths, "/" is the root folder in the jar file, not the root folder of the host machine.

还不是爱你 2024-09-05 02:04:54

我总是不得不困惑 getResourceAsStream 来完成这项工作。如果“resource.xml”位于 org/pablo/opus 中,我认为您想要这样:

Name.class.getResourceAsStream("org.pablo.opus.resource.xml");

I always have to puzzle through getResourceAsStream to make this work. If "resource.xml" is in org/pablo/opus, I think you want this:

Name.class.getResourceAsStream("org.pablo.opus.resource.xml");
愚人国度 2024-09-05 02:04:54

在哪里找到resource.xml?如果位于源树的根目录中,请尝试在其前面添加 / 前缀。

where the resource.xml found? If in the root of the source tree, try to prefix it with /.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文