9

I have an application that was working with Java 1.8.0 162 and I'm in the process of upgrading it to Java 10. One of the issues I'm having is that

appProperties.getClass().getResourceAsStream("/application.properties")

started returning null in Java 10. Any ideas why? appProperties is defined like this:

appProperties = new Properties();

and this happens in a static method, in case that's relevant.

The file is present in src/main/resources/application.properties. This happens whether I'm running in from IntelliJ or from the jar produced by Maven.

I tried adding:

<resources>
    <resource>
       <directory>src/main/resources</directory>
    </resource>
</resources>

to my pom.xml but that had no effect.

Printing the class path with:

System.getProperty("java.class.path")

yields, as the first entry:

C:\Users\pupeno\Documents\Dashman\code\dashman\target\classes

which contains application.properties.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
  • 2
    will this help? https://stackoverflow.com/questions/16570523/getresourceasstream-returns-null – blurfus Apr 06 '18 at 23:43
  • does the javadoc for [ClassLoader.getSystemResourceAsStream()](https://docs.oracle.com/javase/10/docs/api/java/lang/ClassLoader.html#getSystemResourceAsStream(java.lang.String)) makes any sense to you? – Klaimmore Apr 06 '18 at 23:44
  • 1
    @ochi: I'm not using the class of the class, so, it should, I should be using the correct class loader. – Pablo Fernandez Apr 06 '18 at 23:45
  • @Klaimmore: sort of. I see the module system might be into play, so, I'm going to read up on that, but I'm not using `getSystemResourceAsAStream()` nor the system class loader. – Pablo Fernandez Apr 06 '18 at 23:47
  • 1
    If you open the jar, where is the file? – OneCricketeer Apr 06 '18 at 23:47
  • 1
    What does `System.out.println(appProperties.getClass().getResource("/"))` yield? – SeverityOne Apr 06 '18 at 23:49
  • Unable to reproduce it. I have two different setup for JDK 8 and JDK 10, both the place I use idea intelliJ ad maven. I quickly tested it, I couldn't reproduce it. – jmj Apr 06 '18 at 23:50
  • @cricket_007: in the root of the jar. – Pablo Fernandez Apr 06 '18 at 23:51
  • ```java version "10" 2018-03-20 Java(TM) SE Runtime Environment 18.3 (build 10+46) Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10+46, mixed mode)``` – jmj Apr 06 '18 at 23:51
  • @SeverityOne: also null. – Pablo Fernandez Apr 06 '18 at 23:52
  • in your IDEA can you print classpath entries by `System.getProperty("java.class.path")` and for each of the entry go out to terminal and see if it contains `application.properties` file at the root of those entries. – jmj Apr 06 '18 at 23:53
  • @JigarJoshi: I added that to the question. Yes, application.properties is there. – Pablo Fernandez Apr 06 '18 at 23:56
  • @popeno Did you lunch your application from module code? If not did you try? – Andrew Sasha Apr 09 '18 at 08:26

3 Answers3

9

The class of that variable is a system class and it is loaded by a different class loader.

You should use one of your own classes.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Right. `appProperties.getClass().getResourceAsStream(...)` is the equivalent of `Properties.class.getResourceAsStream(...)` so it locates the resource related to java/util/Properties. Moving to `Application.class.getResourceAsStream(...)` so that the resource is related relative to the application class is the right thing to do. – Alan Bateman Apr 14 '18 at 07:50
6

I found a solution although I don't entirely understand why this works an the problematic line doesn't, but this works:

Application.class.getResourceAsStream("/application.properties")

where Application is just a class in my app.

Maybe this is related to the answer pointed to by ochi, and Application.class is using my class loader and appProperties.getClass() is using the system class loader. But why does it behave differently on differently on Java 8 vs 10 is not something that is apparent.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
1

I got the same error while using Intellij and the fix was that I needed to add the file format of the particular resource I intend to fetch. Navigate CRTL+ALT+s or settings->compiler->resource patterns: Enter the file extension into that field. Click apply, then okay

Ercross
  • 519
  • 6
  • 17