开发者

getResource in java web project

开发者 https://www.devze.com 2023-03-06 16:58 出处:网络
the structure of my project contains 3 main folders (like 3 projects included in 1) For example.. Main/java/..

the structure of my project contains 3 main folders (like 3 projects included in 1) For example..

Main/java/..
Main1/java/..
Main2/java/..

in those 3 modules the source folder is java, my problem is how to use getResource method from Main1 to g开发者_如何学JAVAet file url from Main2. Using MyClass.getResource(/xxx/xxx/xxx) is pointing to Main1 root dir.

Thanks in advance.


As javadoc for getResource says

will first search the parent class loader for the resource; if the parent is null the path of the class loader built-in to the virtual machine is searched. That failing, this method will invoke findResource(String) to find the resource.

So, you should you have the class from Main2 in your classpath. An alternative is to implement your own ClassLoader, and implement the findResource method to load classes. Having the class in your classpath seems the more reasonable/easy approach (I personally think that messing with CLassLoaders is generally not a good idea, unless you really know what you're doing).

0

精彩评论

暂无评论...
验证码 换一张
取 消