Menu

#355 Class.forName() doesn't work in interactions pane

3: Ugly
open
5
2008-06-12
2004-12-07
Dan Smith
No

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.

Discussion

  • Mathias Ricken

    Mathias Ricken - 2008-06-12

    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

     
  • Mathias Ricken

    Mathias Ricken - 2008-06-12
    • milestone: --> 3: Ugly
    • assigned_to: nobody --> dlsmith
     
  • Dan Smith

    Dan Smith - 2008-08-28

    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...

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.