I\'ve tried both the example in Oracle\'s Java Tutorials. They both compile fine, but at run-time, both come up with this error:
Exception in thread \"main\"
NoClassDefFoundError in Java:
Definition:
NoClassDefFoundError will come if a class was present during compile time but not available in java classpath during runtime. Normally you will see below line in log when you get NoClassDefFoundError: Exception in thread "main" java.lang.NoClassDefFoundError
Possible Causes:
The class is not available in Java Classpath.
You might be running your program using jar command and class was not defined in manifest file's ClassPath attribute.
Any start-up script is overriding Classpath environment variable.
Because NoClassDefFoundError is a subclass of java.lang.LinkageError it can also come if one of it dependency like native library may not available.
Check for java.lang.ExceptionInInitializerError in your log file. NoClassDefFoundError due to the failure of static initialization is quite common.
If you are working in J2EE environment than the visibility of Class among multiple Classloader can also cause java.lang.NoClassDefFoundError, see examples and scenario section for detailed discussion.
Possible Resolutions:
Verify that all required Java classes are included in the application’s classpath. The most common mistake is not to include all the necessary classes, before starting to execute a Java application that has dependencies on some external libraries.
The classpath of the application is correct, but the Classpath environment variable is overridden before the application’s execution.
Verify that the aforementioned ExceptionInInitializerError does not appear in the stack trace of your application.
Resources:
3 ways to solve java.lang.NoClassDefFoundError in Java J2EE
java.lang.NoClassDefFoundError – How to solve No Class Def Found Error