我的项目结构包含 3 个主要文件夹(例如 1 个中包含 3 个项目)例如..
Main/java/..
Main1/java/..
Main2/java/..
在这3个模块中,源文件夹是java,我的问题是如何使用Main1中的getResource方法从Main2中获取文件url。 使用MyClass.getResource(/xxx/xxx/xxx)指向Main1根目录。
提前致谢。
最佳答案
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.
因此,您应该在类路径中包含 Main2 中的类。另一种方法是实现您自己的 ClassLoader
,并实现 findResource
方法来加载类。将类放在类路径中似乎是更合理/简单的方法(我个人认为弄乱 CLassLoader 通常不是一个好主意,除非您真的知道自己在做什么)正在做)。
关于java - java web项目中的getResource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6016013/