0

I'm trying to load a properties file in a JSF application I'm working on, though I can't manage to reference the file.

package com.nivis.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropHandler {

    String result = "";
    InputStream inputStream;

    public void loadProp() {

        try {

            inputStream = this.getClass().getResourceAsStream("prop.properties");

            if (inputStream == null) {
                System.err.println("===== Did not load =====");
            } else {
                System.err.println("===== Loaded =====");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        PropHandler ph = new PropHandler();
        ph.loadProp();
    }
}

The file is located in the same package and in the different examples I've found when searching for this, that should work. I've also tried to put the file in every conceivable place in the application and reference it to the best of my knowledge, but it does not work.

enter image description here

(only some of the folders that I've tested to put the file)

What am I doing wrong?

Optimally I'd like to have it in the same folder that I use for the msg.properties file.

Tiny
  • 24,933
  • 92
  • 299
  • 571
nivis
  • 873
  • 3
  • 16
  • 32
  • 2
    Did you consider the answers in http://stackoverflow.com/questions/2161054/where-to-place-and-how-to-read-properties-files-in-a-jsp-servlet-web-application – tom Oct 31 '15 at 14:11
  • Did you try `getClass().getResourceAsStream("com/nivis/prop.properties");`? – Zhedar Oct 31 '15 at 14:13
  • @Zhedar: yes, it doesn't work. – nivis Oct 31 '15 at 14:17
  • Now I tried using `/com/nivis/prop.properties` which did work. Strange, I thought I'd already tried that specific combination... Will see if I can get it to work using the same folder as the `msg.properties` file. – nivis Oct 31 '15 at 14:19
  • Use [`ExternalContext#getResourceAsStream(String path)`](http://docs.oracle.com/javaee/7/api/javax/faces/context/ExternalContext.html#getResourceAsStream-java.lang.String-) in JSF and better move the property file to `WEB-INF`, since it is a web resource. – Tiny Oct 31 '15 at 14:27
  • Having removed all `prop.properties` files throughout the app, I deduced that it's actually the one in the same directory as `msg.properties` that is being used - thought it would be the one in the default package. Thanks @Zhedar. – nivis Oct 31 '15 at 14:27
  • That's because `getResourceAsStream` looks for your `resources` directory. It just didnt look in the directories beyond. – Zhedar Oct 31 '15 at 14:35

2 Answers2

2

As this answer elaborates, com/nivis/prop.properties should be the right way to reference the file nested in your resources folder.
But because you're not using ClassLoader classloader = Thread.currentThread().getContextClassLoader(); to locate the Classloader you have to use an absolute path starting with "/" resulting in /com/nivis/prop.properties.

Community
  • 1
  • 1
Zhedar
  • 3,290
  • 1
  • 19
  • 41
0
try something like this

ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("prop.properties");
RamPrakash
  • 1,657
  • 3
  • 20
  • 24
  • Didn't work, I'm afraid. – nivis Oct 31 '15 at 14:21
  • 1
    did you tried all the 3 options @tom specified in the below link http://stackoverflow.com/questions/2161054/where-to-place-and-how-to-read-properties-files-in-a-jsp-servlet-web-application – RamPrakash Oct 31 '15 at 14:30
  • I did try putting the file in most folders, including web-inf. Though I'm not sure exactly which combinations of paths I tried. Anyway, it does work now using `/com/nivis/prop.properties`. Thanks – nivis Oct 31 '15 at 14:34
  • please make sure to mark it as answered so that it helps others with similar issue – RamPrakash Oct 31 '15 at 14:35
  • 3
    Putting a property file in `WEB-INF` is only the matter of doing `InputStream inputStream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/WEB-INF/prop.properties");` in sane JSF applications. – Tiny Oct 31 '15 at 15:02