将相对目录添加到 java 类路径并使用 ClassLoader().getResourceAsStream("")
是否可以将相对目录(即 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。
我无法让它工作,并且不确定这是否只是不受支持,我错过了一些微妙的东西,或者我是否做错了什么。 我需要使用绝对路径吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许我误解了你想要做什么,但是如果你的类路径中有一个文件夹,这意味着它下面的所有文件也应该在类路径中。 如果没有,您始终可以传递类路径上的每个 .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:
And since "foo/bar/plugh" is in the classpath, one of the places it will look for xyz.properties is in "foo/bar/plugh".