Loading properties file within a J2EE application

Discussions

EJB programming & troubleshooting: Loading properties file within a J2EE application

  1. I have written a J2EE application with EJBs and there is a helper class which this EJB extends. From that helper class I am trying to load a property file that is right there along with helper class files in the same folder. I used the following piece of code to get a handle to it but it fails with a NullPointerException.

    java.util.Properties prop = new java.util.Properties();
    prop.load(getClass().getResourceAsStream("xxx.properties"));

    Can someone help me with this?

    thanks in advance

    Sheetal
  2. You have to notice that EJB is running in the EJB container, which the detail implementation is various for different vendor. Hence, it is not good to put the properties file into the same directory as it may be out of context from the point of view of container.
    I recommend you to use the full pathname then it should work fine.
  3. It worked[ Go to top ]

    Thanks Alan, it worked when i used the complete path. i realize your point.

    Sheetal
  4. Sheetal,

    Another approach would be to put the properties in the descriptor file and use JNDI to get your properties. This is more generic the hardcoding the path in your application.

    Just a thought.

    Good luck
  5. In order to make this truly portable and maintainable I would suggest:

    a) Store the properties file in a well known location in the file system and declare the full path name in the JNDI ENC for the EJB we are discussing. Then you may use this full path name when loading the properties file.

    b) Store the properties file inside your deployed .jar or .ear. Now you can reference the properties file by resource name as you indicated in your posting.