ClassLoader.loadClass() Throws java.lang.ClassNotFoundException (Doc ID 1385556.1)

Last updated on NOVEMBER 05, 2016

Applies to:

Java SE JDK and JRE - Version 6 and later
Information in this document applies to any platform.

Symptoms

Let's assume the following snippet:

public class Test {
    public static void main(String[] args) throws ClassNotFoundException {
        Class c =  
            Test.class.getClassLoader().loadClass(byte[].class.getName());
    }
}


During runtime, you get a ClassNotFoundException. In the example stack trace below, the significant strings are printed in bold:

Exception in thread "main" java.lang.ClassNotFoundException: [B
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at Test.main(Test.java:3)


As can be seen, "byte[].class.getName()" returns "[B" which is the name of the byte array class represented by the byte[].class object, as a String. The byte array is just an example.  In practice, you can see any array name in the stack trace.  For example ...

Exception in thread "main" java.lang.ClassNotFoundException: [Cjava.lang.String;


or

Exception in thread "main" java.lang.ClassNotFoundException: [Ljava.lang.Byte;


etc.

Changes

You have upgraded from Java 5 or earlier to Java 6 or later.  In Java 5 there was no problem at all.

Cause

Sign In with your My Oracle Support account

Don't have a My Oracle Support account? Click to get started

My Oracle Support provides customers with access to over a
Million Knowledge Articles and hundreds of Community platforms