将相对目录添加到 java 类路径并使用 ClassLoader().getResourceAsStream("")

发布于 2024-07-18 11:25:35 字数 438 浏览 7 评论 0 原文

是否可以将相对目录(即 foo/bar/plugh)添加到 java 类路径并使用

InputStream in = getClassLoader().getResourceAsStream("xyzzy.properties");

To fetch foo/bar/plugh/xyzzy.properties?

我的类路径如下所示:

foo.jar;foo/bar/plugh;xyz.jar 

我可以使用 foo 和 xyz jar 中的类和资源,但不能使用plugh 目录中的类和资源。 在这些情况下,in 始终为 null。

我无法让它工作,并且不确定这是否只是不受支持,我错过了一些微妙的东西,或者我是否做错了什么。 我需要使用绝对路径吗?

Is it possible to add a relative directory (ie, foo/bar/plugh) to the java classpath and use

InputStream in = getClassLoader().getResourceAsStream("xyzzy.properties");

To fetch foo/bar/plugh/xyzzy.properties?

My classpath looks like this:

foo.jar;foo/bar/plugh;xyz.jar 

And I am able to use classes and resources from both foo and xyz jars but not from the plugh directory. In those cases, in is always null.

I can't get this to work and am not sure if this is just unsupported, I am missing something subtle or if I'm doing something wrong. Do I need to use an absolute path?

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

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

发布评论

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

评论(1

七色彩虹 2024-07-25 11:25:35

也许我误解了你想要做什么,但是如果你的类路径中有一个文件夹,这意味着它下面的所有文件也应该在类路径中。 如果没有,您始终可以传递类路径上的每个 .properties 文件。

但无论哪种方式,由于包含该文件的文件/文件夹位于类路径中,因此您应该能够执行以下操作:

InputStream in = new FileInputStream("classpath:xyz.properties")

并且由于“foo/bar/plugh”位于类路径中,因此它是查找 xyz 的位置之一。属性位于“foo/bar/plugh”中。

Maybe I'm misunderstanding what you're trying to do, but if you have a folder in your classpath, that means all the files underneath it should be in the classpath as well. If not, you can always pass each .properties file on the class path.

But either way, since the file/folder that contains the file is in the classpath, you should just be able to do:

InputStream in = new FileInputStream("classpath:xyz.properties")

And since "foo/bar/plugh" is in the classpath, one of the places it will look for xyz.properties is in "foo/bar/plugh".

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