Home >Java >javaTutorial >Java ClassNotFoundException
As it goes by the name, ClassNotFoundException occurs in Java when a specific class is tried to load by a Java Virtual Machine(JVM). The requested class is not found in the path of the class specified by you, meaning that the path of the class specified by you is broken, which problem is really common in the world of Java. Hence, ClassNotFoundException is also common in Java. This problem is very confusing for those who are beginners in Java, and ClassNotFoundException must be catch or thrown to the caller ClassNotFoundException is a checked exception.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
The syntax ofClassNotFoundException in Java is as follows:
java.lang.ClassNotFoundException:Class_name at location
//a class called program is defined public class Program { //main method is called public static void main(String args[]) { //class not found exception is defined using try and catch block try { // the forname method in class class looks for the mentioned class Class.forName("The Class do not Exist"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
The output of the above program is as shown in the snapshot below:
In the above program, a class called Program is defined. Then the main method is called. Then class not found exception is defined using try and catch block. There is noThe class does not exist.java class which the class loader is trying to find andtheforname method in class looks for the mentioned class, which it fails to find; hence the ClassNotFoundException is thrown. The output of the program is as shown in the snapshot above.
There are several constructors for ClassNotFoundException in Java. They are:
Here are the following examples mention below:
Java program to demonstrate ClassNotFoundException:
Code:
//a class called exceptiondemo is defined public class Exceptiondemo { //a string variable is defined private static final String DRIVE_CLASS = "com.mysql.jdbc.Driver"; //main method is called including the exception public static void main(String[] args) throws Exception { System.out.println("MySQL JDBC driver loading attempt"); //the forname method in class class looks for the mentioned class Class.forName(DRIVE_CLASS); } }
The output of the above program is as shown in the snapshot below:
In the above program, a class called Exception demo is defined. Then the main method is called. Then a string variable is defined to which the JDBC driver path is assigned. Then the main method is called, which throws the exception. The class loader is trying to find the JDBC driver path of the class specified and the forname method in class looks for the mentioned class, which it fails to find; hence the ClassNotFoundException is thrown. The output of the program is as shown in the snapshot above.
Java program to demonstrate ClassNotFoundException(String)
Code:
//a class called check is defined public class Check { //main method is called public static void main(String args[]) { //class not found exception is defined using try catch block try { //the forname method in class class looks for the mentioned class Class.forName("Demonstrating class not found exception"); } catch(ClassNotFoundException e) { //the string specified along with the class not found exception is displayed. System.out.println("There is no class as specified in the path " + e); } } }
The output of the above program is as shown in the snapshot below:
In the above program, a class called check is defined. Then the main method is called. Then the main method is called. Then class not found exception is defined by using try and catch block. Then the forename method in class looks for the mentioned class, which it fails to find; hence the ClassNotFoundException is thrown and the string specified as a detailed message along with the class not found exception is displayed. The output of the program is as shown in the snapshot above.
Steps to avoid ClassNotFoundException:
In this tutorial, we understand the concept of Class Not Found Exception in Java through definition, the syntax of Class Not Found Exception in Java, working of Class Not Found Exception in Java, and their constructors through examples and their outputs.
The above is the detailed content of Java ClassNotFoundException. For more information, please follow other related articles on the PHP Chinese website!