Search code examples
eclipseclassloaderjava-ee-6ear

EAR-Project where to place properties-Files used by EJB-Project using Glassfish3.1.2 with Java EE6


i got a problem with my .properties-files in an EAR project. I already checked some topics on this issue at svf as well as on other boards.

I use properties file for a Util in my EJB-Project, but i can't access them. Refering to a post in a jboss-forum i already tried to put them under META-INF in the EAR-Project and EarContent/lib/classes/ .. sadly it didn't work. I still get this error:

SEVERE: java.util.MissingResourceException: Can't find bundle for base name credentials, locale en_US at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1499) at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1322) at java.util.ResourceBundle.getBundle(ResourceBundle.java:724) at com.lumen.Utils.PropsUtil.loadProperties(PropsUtil.java:50)

I try do access the props-file via: Line 50: ResourceBundle rb = ResourceBundle.getBundle("credentials");

My Project Structure looks like this:

EAR

->EarContent

-->lib

---> someexternallibs.jar

--->classes

---->*.properties

->META-INF

-->*.properties

-->application.xml

EJB

->src

-->package.package.Util <-- here i need to load the properties

Web <-- Here i don't need the properties at all

->src

->WebContent

Add Info: I use eclipse for development, the rest of the project is working so fine so far.

Hope you can help me :)

GreeZ ZuSe


Solution

  • A classes folder is not allowed in a EAR structure.

    So, firstly you should pack your resources in a jar file, either one already existing or a dedicated one in the lib folder.

    Another issue arises regarding the classloader hierarchy if you use the simple:

    ResourceBundle.getBundle(String)
    

    The correct way to call classloader related methods in presence of multiple classloaders is by specifying the current context classloader as in:

    ResourceBundle.getBundle("bundlename",Locale.getDefault(),Thread.currentThread.().getContextClassLoader())