I have a properties file that I’ve included within a jar I’ll be distributing. Before I decided to include the file within the jar I was loading it like
properties.load(new FileInputStream(configFileName));
But this stopped working once the file was placed inside the jar so I changed the code to
properties.load(MyClass.class.getResourceAsStream(configFileName));
Only problem is I have unit tests that use my properties (which are loaded statically so I can’t mock it). The unit tests are run before the jar is made so they all fail now. Is there an elegant way to handle a file that will be in a jar only if the program is run as a jar?
the problem probably is that this file is not visible to your classloader..
i’d use this
pay attention to your classload if this app is a webapplication.. you’ll have many classloaders on this case.. your resource must be visible to this classloader..
in a servlet container like tomcat they work this way
you can read more here
http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html