Search code examples
javaeclipseresourcebundleproperties-file

How can I read properties file in java class located in a folder outside workspace?


I have a properties file located in C:/codebase/myProject-Authentication/sample.properties, and my workspace is at C:/codebase/myProject/com.company.team.website/src/.../AccessCodeServlet.java.

So, I need to read sample.properties from AccessCodeServlet.java. AccessCodeServlet is a servlet.

If the properties file is in the same folder as AccessCodeServlet.java, I can just do this:

ResourceBundle bundle = ResourceBundle.getBundle("sample");

But how do I do it when properties file is outside workspace?


Solution

  • Well, you can add an external folder containing your sample.properties to the classpath in eclipse:

    • Open Run Configuration
    • Select <Your app entry>
    • Go to "Classpath" tab
    • Select "User entries"
    • Click "Advanced" button
    • Check "Add external folder"
    • Click "OK" button
    • Select your external folder with .properties file(s)

    Viola - now resource bundles are found!