Class.forName() bug?

Robert S. Thau rst at ai.mit.edu
Mon Mar 31 12:57:39 PST 1997


In kaffe 0.8.3, it seems that Class.forName() doesn't run the static
initializers of the class it finds.  (See test case and patch below).
A particular consequence of this is that Sun's recommended idiom for
making sure that a particular JDBC driver will be loaded, viz.,

       Class.forName("joes.jdbc.Driver");
       java.sql.Connection c = java.sql.DriverManager.getConnection (
                                "jdbc:eatatjoes://joe.com:8888/burger_db",
                                ...);

doesn't work, since the static initializer in the joes.jdbc.Driver
class which makes it known to the DriverManager (the jdbc central
dispatcher) never gets run by Class.forName.  Changing the forName
native method to call lookupClass instead of loadClass seems to
improve matters.  (My test driver --- the Imaginary mSQL-JAVA code ---
uses new JDK 1.1 API's in a couple of places, but once I kludge around
that, it runs simple queries fine).

A simple test case which seems to show that the static initializers
are just not running is:

  class foo { public static void main (String argv[]) { 
              try {Class.forName ("bar"); } catch (Exception e) { }}}

  class bar { static { System.out.println ("Running static init for bar"); }
              public bar() {}}

The patch (to .../APICore/lib/java.lang/Class.c), for what it's worth:

*** Class.c~	Tue Mar 18 16:43:11 1997
--- Class.c	Mon Mar 31 15:21:14 1997
***************
*** 36,42 ****
  	javaString2CString(str, buf, sizeof(buf));
  	classname2pathname(buf, buf);
  
! 	return loadClass (makeUtf8Const (buf, strlen(buf)), 0);
  }
  
  /*
--- 36,42 ----
  	javaString2CString(str, buf, sizeof(buf));
  	classname2pathname(buf, buf);
  
! 	return lookupClass (buf);
  }
  
  /*



More information about the kaffe mailing list