How to load properties as resources?



Hi,

I done what seems like a lot of searching for an answer to the
following, and was wondering if anyone may be able to shed some light
on it, thanks:

We're using JDK 1.4.2 and Eclipse (on Solaris, if that matters).

Our app uses multiple properties files which are currently on disk.
We're trying to move them into a jar file and load them via
getResourceAsStream(). I've concatenated them all into a single
..properties file named "Common.properties", and put that into a jar
file called "CP.jar". I created the jar file in the same directory
that the .properties file was in, so the properties file shows no path
prefix when I do a "jar tvf" on the jar file.

We have a SystemProperties class that we've written to manage our
properties. In that class's initialization code: I added the
following:

ClassLoader cl = SystemProperties.class.getClassLoader();
if (null != cl)
{
InputStream is = cl.getResourceAsStream("Common.properties");
...more code to load properties from the stream
}


With these additions made to the source, I rebuilt, added "CP.jar" to
the Eclipse project, and tried it. Everything worked wonderfully.

Our app is normally launched via a shell script, so I added "CP.jar" to
the classpath and tried it again, this time with our app being launched
from the shell script.

There were immediate problems, so I configured my setup to be able to
attach Eclipse as a debugger. I saw that getClassLoader() was now
returning null. From what I have been able to find about this, my
understanding is that this indicates that the class is now being loaded
by the Java "bootstrap loader". So, I tried replacing the first line
with:

ClassLoader cl = ClassLoader.getSystemClassLoader();

(I also tried the following line with the same results):

ClassLoader cl = Thread.currentThread().getContextClassLoader();

Either of these calls seem to get me a non-null reference to a
ClassLoader, but now the getResourceAsStream() call is returning null.

Here's what I've tried changing:

- prepending "/" onto the resource name string ("/Common.properties")
- placing the Common.properties file in the same package directory as
our SystemProperties class, and adding it to the jar such that is has
the same path prefix
- prepending "/" to the path used in the previous attempt
- placing the properties file in the same directory where I believe the
application is being launched from
- call SystemProperties.class.getResource("") with the various strings
above to try to get a URL for the resource (always get null)

No matter what I have thought of trying, it seems that once I'm dealing
with the "bootstrap" loader for Java (for whatever reason that is), I
am no longer able to locate the resource.

I've run out of ideas on this, would anyone have a suggestion?

Thanks for your time!

.



Relevant Pages

  • Re: Application wide properties file
    ... The code to open the properties file is inside a package. ... below the one the jar file is in. ... >> This must be a common problem and there is probably a simple method for ...
    (comp.lang.java.programmer)
  • Re: Java app launch dir?
    ... > to have the app look in that directory for a default properties file. ... deployment configuration etc. ... User preferences is updated per session, ... knowledge of the directory of your jar file, ...
    (comp.lang.java.programmer)
  • Re: Find all implementing classes in classpath?
    ... > I'd like to list all classes implementing a certain interface in a UI, ... > to a properties file, ... As classes in a jar file? ... on the file-system, ...
    (comp.lang.java.programmer)
  • Re: ResourceBundle Question
    ... If your properties file is not part of the .jar file put it into root/WEB-INF/classes ... Best regards, ...
    (comp.lang.java.programmer)
  • Possible to find out the path of the resource?
    ... I'm using the below code to get properties from a Properties file ... ... java.util.Properties props = new java.util.Properties; ... ClassLoader cl = this.getClass.getClassLoader; ... I'm using Java 1.5 on Weblogic ...
    (comp.lang.java.programmer)