5

I'm trying to run this code:

public class ClassLoaderTest
{
  public static void main(String[] args) throws Exception
  {
    Object[] obj = new Object[]{};
    String cname = obj.getClass().getName();
    System.out.println(cname);

    ClassLoaderTest.class.getClassLoader().loadClass(cname);
  }
}

But it throws a ClassNotFoundException. Interestingly, if I instead use this line:

Class.forName(cname);

it works just fine.

Whats going on here?

edit: I'm using Java 6. The println prints this:

[Ljava.lang.Object;
Ring
  • 2,249
  • 5
  • 27
  • 40
  • 2
    I guess you ask out of curiosity because in java array "classes" are generated by the vm so it makes no sense to load them. (your would load the component class and then create an array an the vm would create the array class) – morpheus05 May 22 '15 at 22:11
  • 1
    possible duplicate of [Class.forName() vs ClassLoader.loadClass() - which to use for dynamic loading?](http://stackoverflow.com/questions/8100376/class-forname-vs-classloader-loadclass-which-to-use-for-dynamic-loading) – Jonny Henly May 22 '15 at 22:13
  • an array type is indeed a class; and `Class.forName()` javadoc allows it. – ZhongYu May 22 '15 at 23:23

3 Answers3

3

They are not the same at all,

Class.forName return the Class object associated with the class of the given name.

In your example, you give to loadClass a String that represent the name of a class, instead of giving it directly a class.

This method does allow you to give a name, however it must be the binary name of the class, not just the class name.

Any class name provided as a String parameter to methods in ClassLoader must be a binary name as defined by The Java™ Language Specification.

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
1

First, using a class loader to try and load java.lang.Object array is unlikely to work (since java.lang.Object is loaded by the default class loader). Next, the name given by

Object[] obj = new Object[]{};
String cname = obj.getClass().getName();
System.out.println(cname);

is [Ljava.lang.Object;. Clearly that isn't a class that can be resolved by a ClassLoader - the javadoc says (in part) A class loader is an object that is responsible for loading classes; note it does not say it's responsible for loading arrays. In reflection arrays are handled with java.lang.reflect.Array which says, in part, The Array class provides static methods to dynamically create and access Java arrays. which seems to be what you're looking for.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

Looking at the source code around the line that the exception is thrown on, it looks like it is trying to build the filename of the class like this:

String path = name.replace('.', '/').concat(".class");

Given that the value of cname is [Ljava.lang.Object;, I am not especially surprised that the .class file can't be found.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243