How to load properties as resources?
- From: "Mr. Land" <graftonfot@xxxxxxxxx>
- Date: 30 Jan 2006 16:16:33 -0800
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!
.
- Follow-Ups:
- Re: How to load properties as resources?
- From: Roedy Green
- Re: How to load properties as resources?
- Prev by Date: Re: sorry .09 instead of .08
- Next by Date: Re: How to load properties as resources?
- Previous by thread: Beginner CSV error
- Next by thread: Re: How to load properties as resources?
- Index(es):
Relevant Pages
|