properties file in web app

10,395

Take out from META-INF , put it in src/config direct to config package in source , upon build it will go to /WEB-INF/classes/config/config.properties

and this.getClass().getResourceAsStream("/config/config.properties");

Update enter image description here

and then I created a Service Class in the same project which has a method.

public static InputStream getConfigAsInputStream(){
    return Service.class.getResourceAsStream("/config/config.properties");
}

This works..!!

Compare yours

Share:
10,395
Sven
Author by

Sven

Hi, I am a student from Germany. I set my focus on Web Application Development, especially with Java. And I hope this awesome site can help with the awkward behaviour I sometimes cause (: Have a nice day.

Updated on June 04, 2022

Comments

  • Sven
    Sven almost 2 years

    First, there are a lot of solutions out there and I have already read a lot of them. But for some reason I don't get it working.

    I am trying to outsource my config data for my webapp, so that I can cnange it after deployment.

    That is my properties service:

       public class PropertiesService {
    
     Properties properties;
         public PropertiesService() {
          try {
           properties = new Properties();
           ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
           InputStream stream = classLoader.getResourceAsStream("META-INF/config.properties");
           properties.load(stream);
          } catch (Exception e) {
           e.printStackTrace();
          }
         }
    
         public String getHost(){
          return properties.getProperty("server_host");
         }
    
         public String getServerName(){
          return properties.getProperty("server_naming");
         }
        }
    

    After debugging I noticed that the variable stream remains null! But I don't know why -.-

    Need help :-)

    here the error log:

    java.lang.NullPointerException
     at java.util.Properties$LineReader.readLine(Properties.java:418)
     at java.util.Properties.load0(Properties.java:337)
     at java.util.Properties.load(Properties.java:325)
    

    Update

    I do the following now:

    properties.load(this.getClass().getResourceStream("/config/config.properties"));
    

    And I still get a nullPointerException

  • mtraut
    mtraut over 13 years
    Maybe you should add that you don't use the classloader for that - you use the servlet context...
  • mtraut
    mtraut over 13 years
    IN your web app you can request a resource via the ServletContext class as @org.life.java said... Just request the resource in your init method from config.getServletContext().getResource... and add it as a parameter to your class
  • Sven
    Sven over 13 years
    I feel stupid -.- InputStream input = getServletContext().getResourceAsStream("/WEB-INF/config/con‌​fig.properties"); duoesn't work. Is there something I am missing?
  • jmj
    jmj over 13 years
    have you put file at /WEB-INF/config/config.properties ??
  • Sven
    Sven over 13 years
    yes. but I cannot access getServletContext() out of my class. Maybe there is an import missing?! is that possible?
  • jmj
    jmj over 13 years
    OMG.. are you accessing it from simple java class . i thought you are calling it from servlet. anyways. check update
  • Sven
    Sven over 13 years
    Sry no I just created a serviceclass to provide config data. Don't know if that is best practise^^ I updated my initial post. Unfortunately it doesn't work again. I have no clue. Guess I need to surrender because I am running out of time...
  • StartupGuy
    StartupGuy over 10 years
    FWIW, I had conf.properties in /src/main/resources/ (don't remember why) and no matter how I provide the file path (absolute or relative) it just would never work when running the jar calling main(). It would work fine however, when deployed as a servlet. I even added sanity checking code to read the file and print it's contents to the console to make sure I'm not nuts (then close the file) and try to load it, but no joy. Simply moving it to outside /src/main/resources/ to /src/conf/ worked.Why is my code able to read it but properties.load() can't ????
  • StartupGuy
    StartupGuy over 10 years
    I found what the problem was. I am using IntelliJ 12. I simply had to go to Project Structure->Modules->(my module)->Sources and then add src/main/resources as a Context Root (the folder will turn blue).
  • russellhoff
    russellhoff almost 9 years
    Just to comment out that, within the properties file, if you want to store some directory as a variable (then using it so as to let the user choose a place where to download stuff), use double slashes like in here: dirFicherosNuestros=C://Users//IN006//dirFicherosNuestros//