Understanding Thread.currentThread().getContextClassLoader().getResourceAsStream()

Tony picture Tony · Sep 28, 2012 · Viewed 20.7k times · Source

I am looking at a code example, and I am not sure what this means.

Thread.currentThread().getContextClassLoader()
    .getResourceAsStream("MyProperty.properties");

It appears that it looking to read a property file but I am not sure where MyProperty.properties is located.

I appreciate for any help, thanks.

Answer

Gray picture Gray · Sep 28, 2012

It appears that it looking to read a property file but I am not sure where MyProperty.properties is located.

As you currently have it there, that will look for the MyProperty.properties file at the top of your classpath. The could be in your src/main/resources directory or other src folder -- it would depend on how your application (jar/war) is built.

If you are building a jar then you should be able to unpack it and see your properties file at the top level of the jar. If you are building a war, maybe it should be in the WEB-INF/classes directory. Again, it depends on how it was built.

Typically the log4j.properties file is also at the top of the classpath so you may want to look for that file and put your property file alongside it.