
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What are the differences between ClassNotFoundException and NoClassDefFoundError in Java?
Both ClassNotFoundException and NoClassDefFoundError are the errors when JVM or ClassLoader not able to find appropriate class while loading at run-time. ClassNotFoundException is a checked exception and NoClassDefFoundError is an Error which comes under unchecked.
There are different types of ClassLoader loads classes from difference sources, sometimes it may cause library JAR files missing or incorrect class-path which causes loader not able to load the class at run-time.
ClassNotFoundException
ClassNotFoundException comes when we try to load a class at run-time using Reflection and if those class files are missing then application or program thrown with ClassNotFoundException Exception. There is nothing to check at compile-time since it is loading the class at run-time.
Example
public class ClassNotFoundExceptionTest { public static void main(String[] args) { try { Class.forName("Test"); } catch (ClassNotFoundException cnfe) { System.err.println("You are trying to search for a class is not existing. "+cnfe); } } }
Output
You are trying to search for a class is not existing. java.lang.ClassNotFoundException: Test
NoClassDefFoundError
NoClassDefFoundError is thrown when a class has been compiled with a specific class from the classpath but if same class not available during run-time. Missing JAR files are the most basic reason to get NoClassDefFoundError. As per Java API docs "The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found."
Example
class Test1 { public void show() { System.out.println("show() method called"); } } public class Test2 { public static void main(String[] args) { Test1 t = new Test1(); t.show(); } }
When we compile both the classes we will be getting two class files Test1.class and Test2.class, while running Test2 class just remove the Test1.class file then we will be getting NoClassDefFoundError as below
Output
Exception in thread "main" java.lang.NoClassDefFoundError: Test1 at Test2.main(Test2.java:9)
- Related Articles
- What are the differences between length and length () in Java?\n
- What are the differences between JFrame and JDialog in Java?\n
- What are the differences between compareTo() and compare() methods in Java?\n
- What are the differences between a Font and a FontMetrics in Java?\n
- What are the differences between iCloud and SecureSafe?\n
- What are the differences between C++ and Java?
- What are the differences between C and Java?
- What are the differences between an Exception class and an Error class in Java?\n
- What are the differences between the Heap memory and the String Constant Pool in Java?\n
- What are the differences between Java classes and Java objects?
- What are the differences between recursion and iteration in Java?
- What are the differences between GridLayout and GridBagLayout in Java?
- What are the differences between JTextField and JTextArea in Java?
- What are the differences between JRadioButton and JCheckBox in Java?
- What are the differences between StackOverflowError and OutOfMemoryError in Java?
