0

in my maven project, the resources folder look like this:

  • resources
    • images
      • TestImage.png

If I call

System.out.println(MainClass.class.getClassLoader().getResource("images"));

I get "file:/C:/Users/XYZXYZ/IdeaProjects/projectName/core/target/classes/images/"

If I add the file name like this:

System.out.println(MainClass.class.getClassLoader().getResource("images/TestImage.png"));

I get "null". How is this possible? What happens here?

Edit: I noticed that it works (does not give null anymore), as soon as I delete my module-info.java Problem: I need my module-info.java. Any tips?

Lukas Betz
  • 126
  • 8
  • do you have "TestImage.png" in folder "target/classes/images/"? – guleryuz Nov 30 '19 at 09:08
  • @guleryuz Yes, I do. – Lukas Betz Nov 30 '19 at 09:10
  • You probably don't. My guess is that you have `testImage.png`, or `TestImage.png.png`, or `TestImage.PNG`, or something like that. – JB Nizet Nov 30 '19 at 09:13
  • what this prints `System.out.println(MainClass.class.getClassLoader().getResource("images\\TestImage.png"));` ? – guleryuz Nov 30 '19 at 09:13
  • @guleryuz It prints "null" aswell. – Lukas Betz Nov 30 '19 at 09:19
  • @JB Nizet The name is correct. I checked that 3 times and in the end copied the name just to make sure. – Lukas Betz Nov 30 '19 at 09:19
  • What is the output of `dir C:/Users/XYZXYZ/IdeaProjects/projectName/core/target/classes/images/`? – JB Nizet Nov 30 '19 at 09:21
  • @JBNizet where should I put "dir C:/Users ..." ? – Lukas Betz Nov 30 '19 at 09:34
  • In your command line window. Where you execute commands. – JB Nizet Nov 30 '19 at 09:34
  • 30.11.2019 10:37 . 30.11.2019 10:37 .. 03.06.2019 21:59 41.229 TestImage.png – Lukas Betz Nov 30 '19 at 09:42
  • @JBNizet When I delete my module-info.java, it works, can I add anything to my module-info in order to make it work then aswell? – Lukas Betz Nov 30 '19 at 10:13
  • I know nothing about modules, sorry. – JB Nizet Nov 30 '19 at 10:26
  • @JBNizet The question seems to be completely related to the introduction of Java module system with the `module-info.java` class file as pointed by the OP. @TimB. The difference in the execution could be a result of the `ClassLoader` implementation change with respect to the module system as pointed out in [this Q&A](https://stackoverflow.com/questions/46494112/classloaders-hierarchy-in-java-9). One can follow [this](https://stackoverflow.com/questions/46694600/) and the [Javadoc](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/ClassLoader.html) for better understanding. – Naman Dec 01 '19 at 02:26

0 Answers0