tags:

views:

294

answers:

2

I have developed Java desktop application using Netbeans. In my application, I used some properties files and I placed it under the Project folder so that I can access it by using code like that

 Properties props = new Properties();
 props.load(new FileInputStream("config.properties"));

But when I deploy and package my application to .jar file, I can't find out where properties files are and my application can not read value from properties files.

How can I fix that bug, and where should I place my properties file and load it?

+1  A: 

Put it under /src/resources/, then use it like below.

ResourceBundle props = ResourceBundle.getBundle("resources.config");

NetBeans doesn't include the files under your project directory. Further, you need to build the project, in order to let NetBeans put the properties file inside your /classes directory. Then you should be able to pick it up, by running your application or just a particular related Java class.

Cheers.

Adeel Ansari
I followed your guide, but program throw an exception java.util.MissingResourceException: Can't find bundle for base name config, locale en_US at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1521) at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1260)
Chan Pye
Check out my addendum. Precisely, try this `ResourceBundle.getBundle("resources.config")`. Moreover, build the project before running any specific Java class.
Adeel Ansari
+1  A: 

To include a properties file that exists in the classpath (i.e. if it is somewhere under your /src directory when you build the jar file), you can use getResourceAsStream() like this:

Properties properties = new Properties();
properties.load(this.getClass().getResourceAsStream("/config.properties"));

Note the leading slash on the filename. You can also use this to get to files within different packages in your project.

properties.load(this.getClass().getResourceAsStream("/com/company/project/config.properties"));
wpgreenway