this is a maven project, and have one image in resources directory:
├─ src
├─ main
├─ java
└─ resources
└─imgs
└─logo.png
Code:
public class Test {
public static void main(String[] args) {
InputStream stream = Test.class.getClassLoader().getResourceAsStream("/imgs/logo.png");
InputStream stream1 = Test.class.getClassLoader().getResourceAsStream("imgs/logo.png");
System.out.println(stream == null ? "stream is null!" : "stream is not null!");
System.out.println(stream1 == null ? "stream1 is null!" : "stream1 is not null!");
}
}
when I add module-info.java
to project, will print:
stream is null!
stream1 is null!
but when I remove module-info.java
from project, will print:
stream is null!
stream1 is not null!
why? and how to use ClassLoader
to load resources in modular java project?