Search code examples
javaclassloadernoclassdeffounderror

NoClassDefFoundError: A (wrong name: A)


Out of a sudden in a formerly working application, I am getting a NoClassDefFoundError (wrong name) which has puzzled me. I am using an XML binding framework which tries to resolve bound classes at request time by calling ClassLoader.loadClass() with the configured class name. (Why does it behave like that is beyond me in this case.) Now I get the exception just mentioned at java.lang.ClassLoader.defineClass(). The documentation on the method says that it will throw a NoClassDefFoundError if the parameter name is not equal to the binary name of the class specified. I am aware that in that case one expects to get an exception of the form

java.lang.NoClassDefFoundError: A (wrong name: B)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:786)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:144)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:382)
    ....

and this is the outcome I actually see, but the weird thing is that in my case the reported A and B are exactly the same! I tried to debug the app. The line in the JDK source reads:

        c = defineClass1(name, b, off, len, protectionDomain, source);

the value of source is OK. I saved the byte array b to a class file and inspected it with a decompiler and it's OK again. Of course Class.forName() reports the same error throughout, but the really funny thing is that if I set name = null in debug mode I get this beast:

java.lang.LinkageError: loader (instance of  com/google/gwt/dev/shell/jetty/JettyLauncher$WebAppContextWithReload$WebAppClassLoaderExtension): attempted  duplicate class definition for name: "A"
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:786)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:144)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:382)

Now I feel completely clueless on this :( I appreciate your help.

The class loader in question is com.google.gwt.dev.shell.jetty.JettyLauncher.WebAppContextWithReload.WebAppClassLoaderExtension in case that matters.


Solution

  • Solved. Just an embarrassing capitalization issue. The names A and B in NoClassDefFoundError: A (wrong name: B) actually did differ by a capitalization.

    This happens on Windows, because of its case-insensitive file system. If you look for a class named FooBar and the folder contains a file Foobar.class, the classloader opens the class file, but discovers that internally it has a different name. Hence the exception.