After defining the following class in the Definitions
pane and compiling, I have the interaction below.
class MyClass {
static Class lookup() throws ClassNotFoundException {
return Class.forName("MyClass");
}
}
> MyClass.class
class MyClass
> MyClass.lookup()
class MyClass
> Class.forName("MyClass")
ClassNotFoundException: MyClass
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native
Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown
Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown
Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown
Source)
at java.lang.reflect.Method.invoke(Unknown Source)
>
It appears that something is wrong with the ClassLoader
used by Class.forName().
My Interactions classpath is as follows:
C:\Documents and
Settings\Dan\Desktop\drjava-beta-20041124.exe
C:\Program Files\Java\jdk1.5.0\lib\tools.jar
C:\cygwin\home\Dan\Misc [location of MyClass.java]
I've tested this on DrJava versions 20041124-2208 and
20040908-2125 under Windows XP Professional and Java 1.5.0.
Logged In: YES
user_id=666678
Originator: NO
See also:
http://sourceforge.net/tracker/index.php?func=detail&aid=1682371&group_id=44253&atid=438935
Logged In: YES
user_id=1075744
Originator: NO
See also:
https://sourceforge.net/tracker/index.php?func=detail&aid=1857721&group_id=44253&atid=438935
Logged In: YES
user_id=1075744
Originator: NO
As of revision 4513, this problem still exists:
// In Definitions Pane
public class Outer {
public static class Inner {
}
}
// In Interactions Pane
> Outer.class
class Outer
> Outer.Inner.class
class Outer$Inner
> Class.forName("Outer")
java.lang.ClassNotFoundException: Outer
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
> Class.forName("Outer.Inner")
java.lang.ClassNotFoundException: Outer.Inner
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
> Class.forName("java.lang.Integer")
class java.lang.Integer
Logged In: YES
user_id=1060117
Originator: YES
The problem here is that "Class.forName" is defined in terms of the *calling* class's class loader. The calling class is always the interpreter's implementation class (ExpressionEvaluator), and so the corresponding class loader is unrelated to the one associated with the current environment of the interpreter. Like other caller-dependent code (such as exception stack traces), it's hard to fully hide the implementation details of the interpreter. Short of implementing our own JVM, the best we can do is try to force special behavior in certain special cases. But it's debatable whether that's a good idea (it makes program behavior less predictable), and to what extent it should be taken...