
- 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
Why do we get ClassNotFoundException when the class exists in Java?
Whenever we try to load a class, if the class loader is not able to find the class at the specified path a ClassNotFoundException is generated.
This may occur while executing java program, loading a class explicitly using forName() method of the class named Class or the loadClass() method of the ClassLoader class. These two classes accept string values representing the class names and loads the specified classes.
While passing class names to these methods you need to make sure that −
The class names you pass to these methods should be accurate.
The specified class (along with the package) should be either in the current directory or, its path should be listed in the environment variable classpath.
Example
Assume we have created a class named Sample in the directory D:// and compiled as shown below −
package myPackage.example; public class Sample { static { System.out.println("The class named Sample loaded successfully........."); } }
Compilation
D:\>javac -d . Sample.java
This will create a package in the current directory myPackage.example and generates the .class file of the Sample class in it. Therefore, while loading this class, you need to have your it in the same directory and pass the absolute class name to Class.forName() or, loadClass()
Example
public class ClassNotFoundExample { public static void main(String args[]) { try { Class.forName("myPackage.exampl.Sample"); }catch (ClassNotFoundException ex) { ex.printStackTrace(); } } }
Exception
On executing the above program, since the name of the package is wrongly spelt you will get the following exception.
D:\>java ClassNotFoundExample java.lang.ClassNotFoundException: myPackage.exampl.Sample at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source) at java.base/java.lang.ClassLoader.loadClass(Unknown Source) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Unknown Source) at ClassNotFoundExample.main(ClassNotFoundExample.java:4)
If you are trying to access a particular class from another directory you need to set the class path for −
The folder (outer most package) containing the .class file.
or,
The jar file containing the class.
Suppose, we have rectified the spelling issue and trying to load the class from a Java file which is in the directory E://
Example
public class ClassNotFoundExample { public static void main(String args[]) { try { Class.forName("myPackage.example.Sample"); }catch (ClassNotFoundException ex) { ex.printStackTrace(); } } }
Exception
Still you get the same exception since the package containing the .class file of the specified class (or jar file containing it) is neither in the current directory nor in the list of the paths in the environment variable classpath.
E:\>java ClassNotFoundExample java.lang.ClassNotFoundException: myPackage.example.Sample at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source) at java.base/java.lang.ClassLoader.loadClass(Unknown Source) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Unknown Source) at ClassNotFoundExample.main(ClassNotFoundExample.java:4)
In the current scenario set class path to the directory containing the package holding the required class i.e. “D://” and execute the above java program, to make it work.
E:\>javac ClassNotFoundExample.java E:\>java ClassNotFoundExample The class named Sample loaded successfully.........
- Related Articles
- Why do we need a wrapper class in Java?
- When we burn wool why do we get the smell of hair burn?
- When do we we get sun stroke ?
- When we bum nylon, why we do not get the smell of burning paper or burning hair?
- Why do we use import statement in Java? Where it should be included in the class?
- Why do we close our eyes when we sneeze?
- Why Do We Feel Cold When We Have Fever?
- Why do we use interfaces in Java?
- Why do we need generics in Java?
- Why do we need a copy constructor and when should we use a copy constructor in Java?
- Why do we feel hot when we wear winter clothes?
- When can a .class file get created in Java?
- Why do we get more dandruff during winters?
- Why do we get instant energy from glucose?
- Why do we need inner classes in Java?
