3

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

jmj
  • 237,923
  • 42
  • 401
  • 438
Sven
  • 6,288
  • 24
  • 74
  • 116
  • now without the leading slash this should work. double check your classpath, your deployment (is the file really there) and the correct spelling (case). – mtraut Jan 27 '11 at 09:39

1 Answers1

7

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

jmj
  • 237,923
  • 42
  • 401
  • 438
  • Maybe you should add that you don't use the classloader for that - you use the servlet context... – mtraut Jan 27 '11 at 09:24
  • 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 – mtraut Jan 27 '11 at 09:30
  • I feel stupid -.- InputStream input = getServletContext().getResourceAsStream("/WEB-INF/config/config.properties"); duoesn't work. Is there something I am missing? – Sven Jan 27 '11 at 09:42
  • have you put file at `/WEB-INF/config/config.properties` ?? – jmj Jan 27 '11 at 09:47
  • yes. but I cannot access getServletContext() out of my class. Maybe there is an import missing?! is that possible? – Sven Jan 27 '11 at 10:05
  • OMG.. are you accessing it from simple java class . i thought you are calling it from servlet. anyways. check update – jmj Jan 27 '11 at 10:18
  • 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... – Sven Jan 27 '11 at 10:40
  • 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 ???? – Michael M Sep 23 '13 at 22:22
  • 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). – Michael M Sep 24 '13 at 06:34
  • 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// – joninx May 13 '15 at 14:25