ClassNotFoundException in Java

Connect with

ClassNotFoundException in JavaClassNotFoundException in Java is one of the type of runtime exception which throws when java class not found in your path.

1. overview of ClassNotFoundException and NoClassDefFoundError in Java

NoClassDefFoundError and ClassNotFoundException are two similar exception sound on high level in Java but intent of both type of exception are different. Although every developer must have encountered during development days, some are confused, someone understands, someone tries to solve either by googling or another source but one thing is common they encounter this Exception type.

2. When throws ClassNotFoundException in Java

Let us try to understand one by one, ClassNotFoundException exception. When a particular class is not found during the run time, what it means? There can be different scenarios where ClassNotFoundException occurs, when you try to load a class dynamically using different approaches:

  • Class.forName()
  • ClassLoader.loadClass()
  • ClassLoader.findSystemClass()

3. ClassNotFoundException in Java

The ClassNotFoundException throws in java at run time it means while dynamically loading a class using Class.forName() or ClassLoader.loadClass() or ClassLoader.findSystemClass(). This exception occures if you either miss to include your respective .jar or overriden by another .jar, or respective .jar is not included any way in Classpath. In the following program , I tried to get connection but respectiv jar is not included due to any reason. While loading of class “com.microsoft.sqlserver.jdbc.SQLServerDriver” class load did not get and it throws ClassNotFoundException.

package com.mysoftkey.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * In this java class , try to demonstrate, 
 * how java code throws ClassNotFoundException exception
 * 
 * Step: try to get jdbc connect with load MSQL server driver 
 * but without including msql .jar in classpath 
 * 
 * @author ranjeet jha
 *
 */
public class ClassNotFoundExceptionDemo {

  public static void main(String[] args) {
     try {

	 String url = "jdbc:sqlserver://localhost\\SQLEXPRESS;databaseName=testDB";
	
	 // try to load SQLServerDriver into JVM without including respective jar file in the classpath
	 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	 Connection conn = DriverManager.getConnection(url, "password", "password");

      } catch (ClassNotFoundException e) {
	 System.out.println("MS-SQL Driver not loadded");
	 e.printStackTrace();
		
      } catch (SQLException e) {
	e.printStackTrace();
      }
 }

}

4. Console Output for java.lang.ClassNotFoundException

MS-SQL Driver not loadded
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Unknown Source)
	at com.mysoftkey.util.ClassNotFoundExceptionDemo.main(ClassNotFoundExceptionDemo.java:22)

5. Reference

You can visit for more details for Oracle Java Official Docs

Thanks for visiting this page for ClassNotFoundException in Java.
Happy Learning for ClassNotFoundException in Java and You can visit What is ConcurrentModificationException in Java? , you might have encountered this exception in your developement life.

Your comments are welcome to improve this. Happy Learning! 🙂


Connect with

Leave a Comment

Your email address will not be published. Required fields are marked *