classloader and resources.
tgiaccone Dec 10, 2008 4:06 PMThis is my first posting to the JBOSS community forums, if I've picked the wrong topic to post this to, I apologize and please point me to the correct location.
My problem:
I have a utility project for the servlet I'm developing. The artifact produced by this project is a jar file. The jar file gets included in the lib directory of the servlet as part of the war file creation. I have verified that this jar file is in the WEB-INF/lib directory.
In that jar file, I have a number of BIRT report design files. These are files that contain XML that describes a report layout. Each file ends with the extension .rptdesign
If I unarchive the jar file, I end up with three top level directories:
birtReports
com
META-INF
The com hierarchy contains the .class files the birtReport directory contains my birt report templates.
I want to be able to retrieve these xml files in my servlet. To do that I execute the following code:
String name="birtReports/myReport.rptdesign";
InputStream ips = this.getClass().getClassLoader().getResourceAsStream(name))
The result of this is that ips == null.
In an attempt to debug this problem. I added to the jar file, another entry, that creates a file "simple.properties" at the same level as the com and birtReports directories. So now on extraction of the jar into a temp directory I end up with:
ls -1
META-INF
birtReports
mil
simple.properties
If I change the above code to look like this:
name="simple.properties";
InputStream ips = this.getClass().getClassLoader().getResourceAsStream(name))
after executing ips is non-null and I can read from the inputstream to get the contents of the file.
Can someone explain to me, what I'm doing incorrectly? In addition, when reading the documentation on resources, I understand that in some situations you would want to pre-pend a "/" to the resource that you're trying to extract. It's not clear to me where that "root" location is. Can someone explain the use of that "/" for me?
Thanks in advance,
Tony Giaccone