0

I am running a debug session in Eclipse (Kepler). I'm trying to figure out why I can not initialize my PropertyReader. This code runs fine outside of Eclipse.

I have a properties folder that I put my property file in. I verify that I am in fact running out of the folder just above it. Using

String current = new java.io.File( "." ).getCanonicalPath();
System.out.println("Current dir:"+current);
String currentDir = System.getProperty("user.dir");
System.out.println("Current dir using System:" +currentDir);

So on to the code that is confounding me

File testFile = new File("./properties/my.properties");
if (testFile.exists()) {System.out.println("It exists darn it");}
reader = new PropertyReader("./properties/my.properties");

So, the new File and test prints out "It exists darn it" to the console. Showing me that I can read the file. But the next line blows up with the exception

Caused by: java.util.MissingResourceException: Can't find bundle for base name ./properties/my.properties, locale en_US

So what gives? I've tried adding the property file folder to the class path explicitly. I've tried absolute path with the file name, nothing works.

But I can read it with FileReader, File says it exists. This code works fine when compiled. I'm on Fedora 22 and I'm at a loss at why I can see this in code but not with PropertyReader. This happens whether I'm trying to do a debug session or just run it via Eclipse.

the
  • 21,007
  • 11
  • 68
  • 101
  • 2
    What is `PropertyReader`? --- You should also post full stack trace of error. – Andreas Sep 19 '15 at 18:22
  • Please, take a look at this thread: [http://stackoverflow.com/questions/8285595/reading-properties-file-in-java](http://stackoverflow.com/questions/8285595/reading-properties-file-in-java) You will find the answer for your question – Rodrigo Gomes Sep 19 '15 at 18:31
  • Sorry I should have put that up, i was rushed. The line that fails is ResourceBundle.getBundle() where the string passed is the property file name. I've tried with and without .properties on the end. If I do it useing Properties.load() initialied from File with the same string it works fine. – Carl Christianson Sep 19 '15 at 23:09

1 Answers1

0

Two points:

  1. If you want to access properties in a separate folder, then that folder needs to go somewhere on your classpath.
  2. Don't include the ".properties" suffix when referring to it in Java source if you're loading from a java.util.ResourceBundle.

For instance, if your folders are structured like this,

+---bin
¦   ¦   my.properties
¦   ¦
¦   +---com
¦   ¦   +---example
¦   ¦           Main.class
¦   ¦
¦   +---properties
¦           my.properties
¦
+---src
    +---com
        +---example
                Main.java

then you could access the my.properties files from the top level or from the properties folder as follows.

package com.example;

import java.util.ResourceBundle;

class Main {
    public static void main(String[] args) {

        //Get the property from the top level
        ResourceBundle rb = ResourceBundle.getBundle("my");

        //Get the property from properties folder
        ResourceBundle rb2 = ResourceBundle.getBundle("properties/props2");
    }
}

It would be best to keep your properties files in one place though.