1

I am trying to read a properties file in my java web application. I have tried these solution:

Where to place and how to read configuration resource files in servlet based application?

Howto access properties file from Java EE web application?

But none of them worked for me.

Here is the structure of my app:

enter image description here

The code that reads the properties file is placed in the A class and it did not work even I put the absolute path. A is a normal Java class. But everything worked like a charm if the reading properties code is place in the servlet class (ProcessRequest.java)

Here is the code I have used:

public class A {
    public A() {
        try {
            Properties p = new Properties();
            p.load(this.getClass().getClassLoader().getResourceAsStream("/a.properties"));
            String n = p.getProperty("name");
            System.out.println("name: " + n);
        } catch (Exception ex) {
            Logger.getLogger(A.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Any idea?

Community
  • 1
  • 1
ipkiss
  • 13,311
  • 33
  • 88
  • 123

2 Answers2

6

You've put it in the servlets package, however you're trying it to get from the classpath root. The leading / makes the path relative to the classpath root.

Fix the path accordingly:

p.load(this.getClass().getClassLoader().getResourceAsStream("/servlets/a.properties"));

or, assuming that the current class is in servlets package already:

p.load(this.getClass().getClassLoader().getResourceAsStream("a.properties"));

Unrelated to the concrete problem, might it later happen that you move the properties file outside the WAR to an external location which allows easy editing of the file without the need to rebuild/redeploy everytime, then I'd suggest to use the thread's context class loader instead of the current class' class loader. It'll work in all circumstances:

p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("servlets/a.properties"));

(note that the path doesn't need to start with / here, because it's always relative to classpath root)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks, that worked. But how can I read that properties file if I move it to the Web Pages folder? – ipkiss Oct 04 '11 at 15:07
  • 1
    That's answered in the 1st link you found: `ServletContext#getResourceAsStream()`. But I'd think twice if you really need to have it in web content instead of in the classpath. – BalusC Oct 04 '11 at 15:10
  • But that only works if the reading properties code is placed in a servlet class (that we got ServletContext). That does not work in my case, which means the reading code is in a normal class (A.java, there is no such ServletContext in a normal java class) – ipkiss Oct 04 '11 at 15:30
  • Exactly. Just keep it in the classpath as I recommended :) – BalusC Oct 04 '11 at 15:34
0

Do you see the properties file under WEB-INF/servlets after building the application. If yes then try using following line.

p.load(getServletContext().getResourceAsStream("/WEB-INF/servlets/a.properties"));

instead of this

p.load(this.getClass().getClassLoader().getResourceAsStream("/a.properties"));
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
Shamaila Tahir
  • 988
  • 2
  • 8
  • 17