ClassNotFoundException in Java

Connect with

Oracle 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 understand , some one try to solve either by googling or another source but one thing is common they encounter this Exception type.

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()

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 inclding respective jar file in 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();
      }
 }

}

Output with Exception:


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)

Your comments are welcome to improve this. Happy Learning 🙂


Connect with

Leave a Reply

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