简体   繁体   中英

How to load a property file in a Jar when the loader util is a different jar

I am using Property Loader utility that has been packaged as a jar(PropLoader.jar) within my project (MyProject.jar). My property file is in a different package within MyProject.jar ie. com.my.project.resources.MyPropertyFile.properties and I am invoking the PropLoader util (main class: PropLoader) which expects a propfile name in input as:

PropLoader.loadProperties("/com/my/project/resources/MyPropertyFile.properties")

The class file where the above code is in: com.my.project.resources.MyLogic

Internally the utility jar uses getResourcesAsStream.

But this causes a null pointer exception:

SEVERE: java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:418)
    at java.util.Properties.load0(Properties.java:337)
    at java.util.Properties.load(Properties.java:325)

It seems to work absolutely fine when deployed on a different server, or even deployed from withing a different Netbeans workspace!

My workaround for my crummy workspace was to place the property file inside the PropLoader.jar

I also found that the problem with my own workspace was a messed up CLASSPATH. Once I fixed it to reflect my project folder everything worked out fine.

Is the file MyPropertyFile.properties bundled as a resource with your .jar? If not, that is your reason: the loader cannot find the file as it is looking in the .jar for the resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM