How to load resources from base module

Alan Bateman Alan.Bateman at oracle.com
Thu May 18 08:29:40 UTC 2017


On 18/05/2017 08:16, wzberger wrote:

> :
>
> public class ClassA{
>   public ClassA() {
>     //success
> System.out.println(getClass().getResource("/com/b/resources/test.txt"));
The test creates an instance of ClassB (extends ClassA) and so 
getClass() returns ClassB here, not ClassA. So when constructing a 
ClassB then this is equivalent to ClassB.class.getResource(...).

>
>     //fail - null
> System.out.println(ClassA.class.getResource("/com/b/resources/test.txt")); 
>
Right, as this resource is not in the com.a module. Change this to 
ClassB.class.getResource(...) and it should work.

-Alan


More information about the jigsaw-dev mailing list