4

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!

user755806
  • 6,565
  • 27
  • 106
  • 153

2 Answers2

4

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

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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Thanks for your reply and is working fine. But the problem is i have 20 key value pairs in proeprties file. i want them to be loaded in the same order as they are in properties file. But when i get them in servlet, their order is changed. How can i get the key value pairs in the same order how they are in properties file? Thanks! – user755806 Aug 29 '13 at 10:15
  • 1
    You shouldn't rely on that, and since the structure is usually a hashtable, you can't have it. If you want them in order, prefix them with something (1_foo, 2_bar), and then order by that.. But normally the order should not matter – Bozho Aug 29 '13 at 10:17
  • i have to place them in a select box of html. So i need them in order. Please suggest me if there is anything i can do? – user755806 Aug 29 '13 at 10:19
  • @user755806 Then you need to write your own code to read and write the properties file. Java Properties won't maintain order. – Eric Stein Aug 29 '13 at 11:10
3

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();
}
Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
Ashish Chaurasia
  • 1,747
  • 17
  • 23