Thank you for your help in finding my properties file.  Between catching the flu and 
then Christmas activities, I have been slow in
applying the help you so graciously provided.

My key learning points were:

 - Class.getResource(String name) delegates to the class' ClassLoader
 - ClassLoader.getResource(String name) finds the resource with the given name.
 - ResourceBundle.getBundle((String baseName, Locale locale) delegates to the class' 
ClassLoader to find the properties file
associated with the specified Locale.

Alas, I had abstracted the description of my problem too much.  I did not mention that 
I was fighting with JAXB and that it was
JAXB, not my code, that was not able to find its properties file.  I thought that I 
must be doing something wrong in the way I
included the jaxb.properties file in my jar.

The information you provided gave me the clues that helped me to debug the problem in 
JAXB.  I had done nothing wrong.  It was the
JAXB implementation that has a defect in it.

*** The key statement that I found was that "The name of a resource is a "/"-separated 
path name that identifies the resource."


I am working with jaxb-1.0-beta on Win2000.

My JAXB application works just fine when my class files are in the appropriate folders 
in the file system.

I then wrapped up my application in a jar file which includes the file 
com\mycompany\mypackage\jaxb.properties.

Now I get the infamous exception:
javax.xml.bind.JAXBException: Unable to locate jaxb.properties for package 
com.mycompany.mypackage

I learned the following through Web Surfing.

 - jaxb-1.0-beta has a bug.
 - This bug is fixed, but not released.

So I did not bother to upgrade to the latest release of JAXB.

Instead, I proceeded to develop a fix.  Digging deep enough into JAXB, I found that 
the problem was at the following line.
   InputStream is = classLoader.getResourceAsStream( propFileName );

propFileName contains "com\ncems\premis\xml\jaxb\jaxb.properties"

This works just fine for finding the jaxb.properties file in the Win2000 file system.  
It fails to find the jaxb.properties file in
the jar file.

When I changed the propFileName String to contain 
"com/ncems/premis/xml/jaxb/jaxb.properties", it was able to find the
jaxb.properties file in both the jar file and the Win2000 file system.

Time for a kluge.

The last point in my code, before hitting the exception, is the following line.
   JAXBContext jaxb = JAXBContext.newInstance("com.mycompany.mypackage");

newInstance comes in two flavors.  The other one lets me specify the ClassLoader to be 
used to find the jaxb.properties file.  So, I
changed the above line to the following.
   JAXBContext jaxb = JAXBContext.newInstance("com.mycompany.mypackage", new 
JAXBClassLoader());

Now I just needed a well behaved definition for JAXBClassLoader.  The following kluge 
worked for me.

import java.io.InputStream;
import java.net.URL;

public class JAXBClassLoader extends ClassLoader {

  private ClassLoader d_classLoader = Thread.currentThread().getContextClassLoader();

  public InputStream getResourceAsStream(String name) {
    if (name.endsWith("jaxb.properties")) {
      name = name.replace('\\','/');
    }
    return d_classLoader.getResourceAsStream(name);
  }
}

Thanks for your help,
   Harold Meder.


_______________________________________________
Juglist mailing list
[EMAIL PROTECTED]
http://trijug.org/mailman/listinfo/juglist_trijug.org

Reply via email to