1

I have to access my .properties file from its class path. Currently I am accessing that from the resources folder directly. But now I want to acess that from the class path.

Current Code:

   public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
try {
    properties.load(new FileInputStream(
            "src/resources/config.properties"));
    for (String key : properties.stringPropertyNames()) {
        String value = properties.getProperty(key);
        rate.add(value);
    }
}
}

Path of the file is :src/resources/config.properties

For deploying the code we are creating war file of the complete project.

Please suggest how can we get this file from the class path.

Developer
  • 6,292
  • 19
  • 55
  • 115

3 Answers3

3

If your config.properties file reside on your same package of your java class then just use,

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config.properties");

If you put the properties file in any package, then use the package name also

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("com/your_package_name/config.properties");

So the compete code be like,

try {
    Properties configProperties = new Properties();
    InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("resources/config.properties");
    configProperties.load(inputStream);
}
catch(Exception e){
    System.out.println("Could not load the file");
    e.printStackTrace();
}

UPDATE : For better understanding see the image.

enter image description here

Ataur Rahman Munna
  • 3,887
  • 1
  • 23
  • 34
0

You can load file from classpath using this

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

If you are dealing with .properties files you should consider using ResourceBundle

  • in the load i have to do the chages ?? – Developer Apr 18 '16 at 06:25
  • you can do it this way `properties.load(this.getClass().getClassLoader().getResourceAsStream("config.properties"));` –  Apr 18 '16 at 06:26
  • i am getting null pointer exception properties.load(this.getClass().getClassLoader().getResourceAsStream("config.pr‌​operties")); – Developer Apr 18 '16 at 07:46
  • can you help me getClass().getClassLoader().getResourceAsStream() about this line – Developer Apr 18 '16 at 09:23
  • `this.getClass().getClassLoader().getResourceAsStream()` is creating an InputStream from a file located in the resources folder. If you're getting a null pointer exception, it means that the file is not found in the `resources` folder. Is the calling class in the same classpath as your config.properties ? –  Apr 18 '16 at 10:52
0
final Properties properties = new Properties();
try (final InputStream stream =
           this.getClass().getResourceAsStream("somefile.properties")) {
    properties.load(stream);
}
Raman Shrivastava
  • 2,923
  • 15
  • 26
  • While this code may answer the question, it would be better to include some context, explaining how it works and when to use it. Code-only answers are not useful in the long run. – Bono Apr 18 '16 at 09:19