Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load properties file in servlet?

Tags:

java

jsp

servlets

I have a java maven project. i have placed a properties file in src/main/resources folder.

src/main/resources
  |
  |___properties
        |
        |
        |___custom_en_US.properties

I am loading properties file as below in servlet.

ResourceBundle bundle = ResourceBundle.getBundle("classpath:properties/custom", request.getLocale());

but above line is throwing exception saying resource not found. How can i give path to properties file? Please help me.

Thanks!

like image 785
user755806 Avatar asked Mar 22 '23 14:03

user755806


2 Answers

Get rid of the "classpath" prefix.: .getBundle("/properties/custom")

The "classpath" prefix is not a standard, it is defined by some frameworks like spring.

like image 151
Bozho Avatar answered Apr 01 '23 10:04

Bozho


Read the property file from classpath

Properties prop = new Properties();

try {
    //load a properties file from class path, inside static method
    prop.load(App.class.getClassLoader().getResourceAsStream("config.properties"));            
} catch (IOException ex) {
    ex.printStackTrace();
}
like image 31
Ashish Chaurasia Avatar answered Apr 01 '23 11:04

Ashish Chaurasia