1

In the web application, which I am going to build, I need some SQL queries, which I can store in a properties file, and get them into my Java code, and then execute.

I have found many suggestions on the internet, but nothing helped me. Some of them are

this.getClass().getClassLoader().getResourceAsStream("resources/file.properties");
this.getClass().getResourceAsStream("resources/file.properties");

I have kept the properties file in resources, and in the same directory in which my Java file is present. Nothing worked. I am using Eclipse and Struts2.

2
  • Where do you want to use the values from the properties file? in your action class? If so, there is a struts way of doing it.
    – Elye M.
    Commented Feb 3, 2015 at 16:14
  • I need to read the sql query in Dao.
    – Pawan
    Commented Feb 4, 2015 at 3:57

1 Answer 1

3

Make sure that your properties files goes to WEB-INF\classes, where your struts.xml is. This folder is on the classpath. Then

this.getClass().getClassLoader().getResourceAsStream("file.properties");

should work as expected. Or of course, if you create a resources folder in classes, the aforementioned code should work too.

If by resources folder you mean src/main/resources, then it is managed by Maven, and it is copied directly to WEB-INF/classes. So you do not need to specify the resorces folder in the method.

4
  • I kept it in the same place(resources) where struts.xml file is present. Still I am getting "java.lang.NullPointerException"
    – Pawan
    Commented Feb 3, 2015 at 16:10
  • I am just using struts2 and tiles.
    – Pawan
    Commented Feb 3, 2015 at 16:11
  • My project is working fine when I placed SQL queries in the java directly. Now I want to keep them in separate file and use in java.
    – Pawan
    Commented Feb 3, 2015 at 16:14
  • Could you add a print screen about your project layout to the question? Also make sure that the properties file is actually in the WAR file! Commented Feb 3, 2015 at 16:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.