So, I’m trying out the apache log4j library and I managed to get everything working correctly. I ran my program once and it did all of the logging just perfectly. Then, I went to run it again and it crashed. I tried again and again the program crashed.
I have a few .properties files in a properties/ directory. I’m using two different .properties files and loading them both using getResource and getResourceAsStream respectively:
first in order to configure my logger:
PropertyConfigurator.configure(MyClass.class.getResource("properties/MyClassconfig.properties"));
then in order to load all of the constants used in the program:
properties.load(MyClass.class.getResourceAsStream("properties/constants.properties"));
As I said before, on the first run this worked perfectly. From the second run, I got this error:
log4j:ERROR Could not read configuration file from URL [null].
java.lang.NullPointerException
at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:522)
at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:415)
at com.package.package2.common.main.MyClass.main(MyClass.java:69)
log4j:ERROR Ignoring configuration file [null].
So, the line:
MyClass.class.getResource("properties/MyClassconfig.properties")
is no longer returning a valid URL, but instead it’s returning null. I was thinking this was because some InputStream remained open after the first run (like the input stream opened in:
MyClass.class.getResource("properties/MyClassconfig.properties"); )
The reason I think there is a conflict between getResource and getResourceAsStream is because the crash actually happens later when I call:
properties.load(MyClass.class.getResourceAsStream("properties/constants.properties"));
I get:
Properties$LineReader.readLine() line: not available [local variables unavailable] Properties.load0(Properties$LineReader) line: not available Properties.load(InputStream) line: not available
Even though there is no link between the logger itself and the Properties.load() and its InputStream (other than that they are both initialized with .properties files)
Why not just have
log4j.propertiesin your classpath and do away with all the manual resource loading?