2

I would like to get DB connection parameters from a properties file in a static block. The properties file location is WEB-INF/classes/db.properties.

I will prefer to use getResourceAsStream() method. I have tried many ways, but they all returned null.

private static Properties prop = new Properties();
static{
    try {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();
        InputStream inputStream = servletContext.getResourceAsStream("/db.properties"); 
        InputStream is = prop.getClass().getResourceAsStream("/db.properties");
        if(inputStream!=null){//it is null
            prop.load(inputStream);
        }
        if(is!=null){//it is null
            prop.load(is);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

How is this caused and how can I solve it?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Viola
  • 47
  • 1
  • 2
  • 6
  • 1
    http://jaitechwriteups.blogspot.ca/2007/01/how-to-read-properties-file-in-web.html – Thufir Apr 01 '12 at 20:54
  • Possible duplicate of [Where to place and how to read configuration resource files in servlet based application?](http://stackoverflow.com/questions/2161054/where-to-place-and-how-to-read-configuration-resource-files-in-servlet-based-app) – BalusC Jun 07 '16 at 11:12

2 Answers2

1

As Thufir wrote in a comment, there is a nice tutorial from reading properties from Java code: http://jaitechwriteups.blogspot.ca/2007/01/how-to-read-properties-file-in-web.html

/** 
 * Some Method 
 *  
 * @throws IOException 
 *  
 */  
public void doSomeOperation() throws IOException {  
    // Get the inputStream  
    InputStream inputStream = this.getClass().getClassLoader()  
            .getResourceAsStream("myApp.properties");  

    Properties properties = new Properties();  

    System.out.println("InputStream is: " + inputStream);  

    // load the inputStream using the Properties  
    properties.load(inputStream);  
    // get the value of the property  
    String propValue = properties.getProperty("abc");  

    System.out.println("Property value is: " + propValue);  
}  
Lukasz Czerwinski
  • 9,482
  • 8
  • 45
  • 56
1
InputStream inputStream = servletContext.getResourceAsStream("/db.properties"); 

This attempt expects the file to be in /WebContent/db.properties.

InputStream is = prop.getClass().getResourceAsStream("/db.properties");

This attempt expects it to be in at least the same archive (JAR) as the java.util.Properties class.

Neither of those attempts reads the file which you've placed in /WEB-INF/classes/db.properties. You can fix this problem in basically 2 ways.

  1. Move it directly in the /WEB-INF folder as /WEB-INF/db.properties and load it as follows:

    InputStream input = externalContext.getResourceAsStream("/WEB-INF/db.properties");
    

    (note that you don't need to haul the ServletContext from under the JSF's hoods; there's already a delegate method for that)

  2. Load it relative to the class which is also present in /WEB-INF/classes, e.g. the current managed bean class.

    InputStream input = Bean.class.getResourceAsStream("/db.properties");
    

    Or just use the context classloader, it has access to everything.

    InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
    

    (note the lack of the / prefix)

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452