1

I have a class named as MyClass, so when i called classloader.loadClass("myclass") throws a NoClassDefFoundError instead of a ClassNotFoundException.

Note - I have changed the upper case letter to small case letter

When i used completely different string "ABC" i get ClassNotFoundException as expected .

Please find the code snippet below.

    public class Test {

    public static void main(String[] args) {
        // normal conditions
        try {
            Test.class.getClassLoader().loadClass("MyClass");
            System.out.println(" Successfully loaded an existing class ");
        } catch (ClassNotFoundException notExpected) {

        }

        // expected
        try {
            Test.class.getClassLoader().loadClass("ABC");
        } catch (ClassNotFoundException expected) {
            System.out.println("Expected");
        }

        try {
            Test.class.getClassLoader().loadClass("myclass");
        } catch (ClassNotFoundException expected) {
            System.out.println("Expected but not coming");
        }
    }
}

//

// sample class used for the above in separate file
// 
public class MyClass
{ 

}

OutPut:

Successfully loaded an existing class 
Expected
Exception in thread "main" java.lang.NoClassDefFoundError: myclass (wrong name: MyClass)

[![Output on Java 8 Machine][1]][1]

It is not duplicate as I am looking for different exceptions on name change scenario please check again

T-Bag
  • 10,916
  • 3
  • 54
  • 118
  • 1
    I think this is valuable SO post which is highly relevant to your problem (might be even duplicate): http://stackoverflow.com/questions/10890805/case-sensitivity-of-java-class-names – Pavel Vasilev Apr 03 '17 at 05:14
  • Possible duplicate of [Case sensitivity of Java class names](http://stackoverflow.com/questions/10890805/case-sensitivity-of-java-class-names) – Philipp Wendler Apr 03 '17 at 05:24

2 Answers2

0

I've copied your program as is and run on my machine (Java jdk1.8.0_121 on Windows 10). The output it has produced is:

Expected
Expected but not coming

So probably you have some previous parts of your tests that have not been deleted or alternatively you're running on a jvm that has a bug that has been fixed in more recent version (unlikely to be so but the possibility still exists).

There is a pretty nice article on those exceptions: available here

So NoClassDefFoundError (its an Error, not just an exception, btw!) is thrown when the class was available during compile time, but later on has been removed from the classpath in runtime. JVM really counts on this class, but it doesn't exist so JVM is forced to throw an error like this.

Could you please recompile your project and make sure that you don't really have "myclass.java" as a file on disk during the compilation?

Another option I would check is upgrading your JVM to the most recent version and trying again.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • Please check attached image . – T-Bag Apr 03 '17 at 05:17
  • yes, I believe you :) Are you running inside eclipse or something? If so - could you please try to run it with java.exe directly? – Mark Bramnik Apr 03 '17 at 05:21
  • Did you add MyClass.java in your package ?? – T-Bag Apr 03 '17 at 05:24
  • No I didn't otherwise I'll probably get your behaviour. In windows classes are not case sensitive and your IDE probably pre-caches compiles classes. That's why I've asked about running your test outside the IDE (clean env) – Mark Bramnik Apr 03 '17 at 05:52
0

There is a fundamental difference between the ClassNotFoundException which the ClassLoader uses to report an absent class immediately when a loadClass attempt is made and the subtypes of LinkageError which are thrown when the class has been found, but structural constraints are violated or dependencies could not be resolved.

These subtypes of LinkageError (which includes NoClassDefFoundError) might be even thrown at a much later time, due to the lazy resolving/verification.

So in your case, a class file has been loaded, apparently because your operating system or file system is not case sensitive, but the NoClassDefFoundError has been thrown within defineClass due to the mismatching name (note that this method takes the expected name as parameter).

As explained in this answer, you get a system independently consistent behavior, if you store the class files into a jar file instead of relying on the file system.

Community
  • 1
  • 1
Holger
  • 285,553
  • 42
  • 434
  • 765