[kaffe] ClassLoader isn't working like I expected

Tom Epperly tepperly@llnl.gov
Tue May 13 08:20:01 2003


I have some DTDs for XML parsing that I add to the jar file with the rest of 
my code. It's convenient to put them in the jar file; otherwise, I need to 
know what directory they are installed in.

My code uses getClass().getClassLoader().getResourceAsStream() to get an 
InputStream for the DTD files. This approach works with Sun's JVM, and it 
doesn't work with the Kaffe that I installed from Debian x86 unstable 
distribution. I've included a simple test case below to demonstrate the issue.

If there is a better (portable between Kaffe and Sun's JVM) way for me to 
get an InputStream for the DTD files in my JAR file, I would love to hear 
it. Please Cc me on replies.

Thanks in advance,

Tom

To demonstrate this issue, you need two files in directories as follows:

	LoaderProblem/Test.java
	dtd/SIDL.dtd

//----begin Test.java----
package LoaderProblem;
import java.io.InputStream;

public class Test {

   public static void main(String [] args) {
     try {
       Test t = new Test();
       ClassLoader loader = t.getClass().getClassLoader();
       InputStream in = loader.getResourceAsStream("dtd/SIDL.dtd");
       if (in != null) {
         byte [] buffer = new byte[256];
         int len;
         while ((len = in.read(buffer, 0, 255)) >= 0) {
           System.out.write(buffer, 0, len);
         }
       }
     }
     catch (java.io.IOException ioe) {
       ioe.printStackTrace();
     }
   }
}
//-----end Test.java----

dtd/SIDL.dtd can be any short text file. For example,

//----start SIDL.dtd
Hello world
//----end SIDL.dtd

% javac LoaderProblem/Test.java
% jar cf lp.jar LoaderProblem/Test.class dtd/SIDL.dtd
% /usr/local/j2sdk1.4.1_02/bin/java -classpath lp.jar LoaderProblem.Test
Hello world
% kaffe -addclasspath lp.jar LoaderProblem.Test
java.lang.NullPointerException
         at LoaderProblem.Test.main(Test.java:20)
% kaffe -version
Kaffe Virtual Machine

Copyright (c) 1996-2002 Kaffe.org project contributors (please see
   the source code for a full list of contributors).  All rights reservced.
Portions Copyright (c) 1996-2002 Transvirtual Technologies, Inc.

The Kaffe virtual machine is free software, licensed under the terms of
the GNU General Public License.  Kaffe.org is a an independent, free software
community project, not directly affiliated with Transvirtual Technologies,
Inc.  Kaffe is a Trademark of Transvirtual Technologies, Inc.  Kaffe comes
with ABSOLUTELY NO WARRANTY.

Engine: Just-in-time v3   Version: 1.0.7   Java Version: 1.1
% uname -a
Linux driftcreek 2.4.20 #11 Tue Jan 21 08:44:00 PST 2003 i686 unknown 
unknown GNU/Linux
% dpkg --list kaffe
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Installed/Config-files/Unpacked/Failed-config/Half-installed
|/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad)
||/ Name           Version        Description
+++-==============-==============-============================================
ii  kaffe          1.0.7-3        A JVM to run Java bytecode