简体   繁体   English

Class.getResource在错误的jar中查找资源

[英]Class.getResource finds resource in wrong jar

  • ClassA is located within JarA ClassA位于JarA中
  • ResourceA is located within JarA ResourceA位于JarA中
  • ResourceB is located within JarB ResourceB位于JarB中

Both jars are in the classpath. 两个罐子都在类路径中。

ResourceA and ResourceB have the same path within their respective jars, ie "resources/somefile.txt" ResourceA和ResourceB在各自的jar中具有相同的路径,即“ resources / somefile.txt”

Using: 使用方法:

ClassA.class.getResource("resources/somefile.txt");

I would expect to always get ResourceA however I am getting ResourceB. 我希望总是得到ResourceA,但是我得到ResourceB。

Is this expected behaviour, and if so is there any way to garauntee returning ResourceA in this scenario? 这是预期的行为吗?如果是这样,在这种情况下有什么方法可以保证保证人返回ResourceA?

Thanks in advance. 提前致谢。

This is related to classloaders. 这与类加载器有关。 One trick to overcome such issue is to use something like this: 解决此类问题的一种技巧是使用如下所示的内容:

A.class.getClassLoader().getResource("/yourResources")

Where A is some class in the desired archive. 其中A是所需归档中的某个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM