1

How to load property files placed in resource folder of an executable jar file. Here my app itself is a jar and it executes on it own. It need to find this property file (placed within itself under resource folder) at runtime depending on the path mentioned in the code. I have used below two methods but it didn't help me. Point here is, both these options are working fine when i execute in eclipse, but doesn't work when I pack it into an executable jar. It throws NullPointerException. Only problem I see here is that jar is not able to pick the property files with given path. Any help would be appreciated.

Method 1: Using Apache Commons Configuration

 URL propFileURL = XYZ.class.getClassLoader().getResource("/config.properties");
Configuration propertyConfiguration = null;             
propertyConfiguration = new PropertiesConfiguration(propFileURL);

In above case I'm getting ConfigurationException. Class is not able to find file mentioned in given path.

Method 2: Using getResourceAsStream. I know that getResource doesn't work if we are to load files from network on in any other location.

InputStream is =XYZ.class.getClassLoader().getResourceAsStream("/config.properties");
Properties prop = new Properties();
prop.load(is);

In this case, I'm getting nullPointerException. Let me know if you need more details.

jar content Heirarchy

Properties file - /java-file-io-application/src/main/resources/config.properties
XYZ class - /java-file-io-application/src/main/java/org/bc/xyz/iplugin/utilities/XYZ.java
1CzDx
  • 57
  • 1
  • 2
  • 8
  • Show us your jar content hierarchy. – Sotirios Delimanolis Jun 12 '14 at 19:55
  • Updated the question. Could you please check! – 1CzDx Jun 12 '14 at 20:00
  • Looks like your `jar` file is not packaged properly. In the current form you should give `/java-file-io-application/src/main/resources/config.properties` as a path to `getResource()` or `getResourceAsStream()` – Alexander Pogrebnyak Jun 12 '14 at 20:04
  • I think you need to use `("resources/config.properties")` as your properties file is in the resources folder. Have you tried that? – Michael Freake Jun 12 '14 at 20:12
  • @AlexanderPogrebnyak I'm still getting NullPointerException. – 1CzDx Jun 12 '14 at 20:27
  • @MichaelFreake `resources/config.properties` actually worked in my case. Thanks for the help. I think this is because as we are using getResource method, jar will know path till resource files. It's just that We just need to specify further hierarchy if we have any. – 1CzDx Jun 12 '14 at 20:28
  • @talk2sharn perfect! Happy to hear you have it working. Keep in mind that just because you are using a `resources` folder, java isn't intelligent enough to realize that. In other words, `getResource` doesn't automatically mean look in a folder called "resources". – Michael Freake Jun 12 '14 at 21:02

1 Answers1

2

Looks like you might be building your jar incorrectly. Files from 'src/main/resources' would be expected at the root of the jar file. If your jar file contains the 'src/main/resources' directory, something's off with your build.

beirtipol
  • 823
  • 5
  • 21