简体   繁体   中英

getResource in java web project

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 get 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 CLassLoader s is generally not a good idea, unless you really know what you're doing).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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