Class.forName() with dynamic String

I'm trying to create objects of a class whose name depends on a runtime database lookup. So my code looks like this:

package com.mycompany.ourproduct.ourpackage;

. . .

String className = rs.getString(1);

Class c = Class.forName(className);

. . .

When I run the program, I get a ClassNotFoundException. But everything runs fine if I replace the code above with this:

package com.mycompany.ourproduct.ourpackage;

. . .

// String className = rs.getString(1);

String className = "com.mycompany.ourproduct.ourpackage.oursubpackage.MyClass"

Class c = Class.forName(className);

. . .

The problem seems to be in the dynamic linking. Although the class I'm trying to load is in a different package, it is a public class (and I can load it with a static string). Neither does moving it to the same package fix the problem.

I've noticed that if I supply a mis-named class with a static string, the exception reads:

java.lang.ClassNotFoundException: com.mycompany.ourproduct.misspelledpackage.SomeClass

but the same exception prints as follows when the string comes from the database:

java.lang.ClassNotFoundException: com/mycompany/ourproduct/misspelledpackage/SomeClass

I thought the difference was one of compile-time linking and run-time linking, but oddly enough, I still get the dots for misspelled classes that I (1) supply on the command line, (2) read from a file, or (3) concatenate from command line arguments plus info from a file. It's just when I pull the class name from the database that I get the slashes. Does anyone know what's going on?

Here's what I get from java -version:

java version "1.2.2"

Solaris VM (build Solaris_JDK_1.2.2_05a, native threads, sunwjit)

Any help would be greatly appreicated!

[1875 byte] By [pjungwirth] at [2007-9-26 2:56:44]
# 1

Hmm. Weird. Have you looked at the String you're getting back from the database? Does it have slashes or dots? (Should be dots.) Have you made sure any leading/trailing spaces have been trimmed? Try doing classname.equals("com.mycompany... etc.") to see if it really is the same string as the constant that does work.

If it turns out the String is correct, and the only difference is whether that String came from a databaes or a literal, then I don't know what's going on. The first step, though, is to verify whether that's really the case, or if you're not getting the String you think you are.

jverd at 2007-6-29 10:48:06 > top of Java-index,Core,Core APIs...
# 2

I ended up comparing the ascii of the database result and the literal string, and it turns out the database was giving me a C-style null-terminated string. That was enough to cause the class load to fail. What's JDBC doing returning null-terminated strings? From now on I will .trim() everything I get back! Or maybe I'll write a jdbcChomp().

Thanks for your help!

pjungwirth at 2007-6-29 10:48:06 > top of Java-index,Core,Core APIs...