Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

java.lang.ClassNotFoundException: How to resolve ~ gniithelp

Java.lang.ClassNotFoundException: How To Resolve ~ Gniithelp
This article is intended for Java beginners facing java.lang.ClassNotFoundException challenges. It will provide you with an overview of this common Java exception, a sample Java program to support your learning process and resolution strategies.

If you are interested on more advanced class loader related problems, I recommended that you review my article series onjava.lang.NoClassDefFoundError since these Java exceptions are closely related.

java.lang.ClassNotFoundException: Overview

As per the Oracle documentation, ClassNotFoundException is thrown following the failure of a class loading call, using its string name, as per below:

  • The Class.forName method
  • The ClassLoader.findSystemClass method
  • The ClassLoader.loadClass method
In other words, it means that one particular Java class was not found or could not be loaded at “runtime” from your application current context class loader.

This problem can be particularly confusing for Java beginners. This is why I always recommend to Java developers to learn and refine their knowledge on Java class loaders. Unless you are involved in dynamic class loading and using the Java Reflection API, chances are that the ClassNotFoundException error you are getting is not from your application code but from a referencing API. Another common problem pattern is a wrong packaging of your application code. We will get back to the resolution strategies at the end of the article.

java.lang.ClassNotFoundException: Sample Java program

* A ClassNotFoundException YouTube video is now available.
Now find below a very simple Java program which simulates the 2 most common ClassNotFoundException scenarios via Class.forName() & ClassLoader.loadClass(). Please simply copy/paste and run the program with the IDE of your choice (Eclipse IDE was used for this example).

The Java program allows you to choose between problem scenario #1 or problem scenario #2 as per below. Simply change to 1 or 2 depending of the scenario you want to study.

# Class.forName()
private static final int PROBLEM_SCENARIO = 1;

# ClassLoader.loadClass()
private static final int PROBLEM_SCENARIO = 2;


# ClassNotFoundExceptionSimulator
package org.ph.gniithelp.training5;

/**
 * ClassNotFoundExceptionSimulator
 * @author Pierre-Hugues Charbonneau
 *
 */
public class ClassNotFoundExceptionSimulator {
      
       private static final String CLASS_TO_LOAD ="org.ph.gniithelp.training5.ClassA";
       private static final int PROBLEM_SCENARIO = 1;
      
       /**
        * @param args
        */
       public static void main(String[] args) {
            
             System.out.println("java.lang.ClassNotFoundException Simulator - Training 5");
             System.out.println("Author: Pierre-Hugues Charbonneau");
             System.out.println("http://gniithelp.blogspot.com");
            
             switch(PROBLEM_SCENARIO) {
                   
                    // Scenario #1 - Class.forName()
                    case 1:
                          
                           System.out.println("\n** Problem scenario #1: Class.forName() **\n");
                           try {
                                 Class> newClass = Class.forName(CLASS_TO_LOAD);
                                
                                 System.out.println("Class "+newClass+" found successfully!");
                          
                           } catch (ClassNotFoundException ex) {
                                
                                 ex.printStackTrace();
                                
                                 System.out.println("Class "+CLASS_TO_LOAD+" not found!");
                          
                           } catch (Throwable any) {                           
                                 System.out.println("Unexpected error! "+any);
                           }
                          
                           break;
                          
                    // Scenario #2 - ClassLoader.loadClass()
                    case 2:
                          
                           System.out.println("\n** Problem scenario #2: ClassLoader.loadClass() **\n");                     
                           try {
                                 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();            
                                 Class> callerClass = classLoader.loadClass(CLASS_TO_LOAD);
                                
                                 Object newClassAInstance = callerClass.newInstance();
                                
                                 System.out.println("SUCCESS!: "+newClassAInstance);
                           } catch (ClassNotFoundException ex) {
                                
                                 ex.printStackTrace();
                                
                                 System.out.println("Class "+CLASS_TO_LOAD+" not found!");
                          
                           } catch (Throwable any) {                           
                                 System.out.println("Unexpected error! "+any);
                           }
                          
                           break;
             }
            
             System.out.println("\nSimulator done!");
       }
}


# ClassA
package org.ph.gniithelp.training5;

/**
 * ClassA
 * @author Pierre-Hugues Charbonneau
 *
 */
public class ClassA {
      
private final static Class CLAZZ = ClassA.class;
      
       static {
             System.out.println("Class loading of "+CLAZZ+" from ClassLoader '"+CLAZZ.getClassLoader()+"' in progress...");
       }
      
       public ClassA() {
             System.out.println("Creating a new instance of "+ClassA.class.getName()+"...");
            
             doSomething();
       }
      
       private void doSomething() {           
             // Nothing to do...
       }
}


If you run the program as is, you will see the output as per below for each scenario:

#Scenario 1 output (baseline)
java.lang.ClassNotFoundException Simulator - Training 5
Author: Pierre-Hugues Charbonneau
http://gniithelp.blogspot.com

** Problem scenario #1: Class.forName() **

Class loading of class org.ph.gniithelp.training5.ClassA from ClassLoader 'sun.misc.Launcher$AppClassLoader@bfbdb0' in progress...
Class class org.ph.gniithelp.training5.ClassA found successfully!

Simulator done!

#Scenario 2 output (baseline)
java.lang.ClassNotFoundException Simulator - Training 5
Author: Pierre-Hugues Charbonneau
http://gniithelp.blogspot.com

** Problem scenario #2: ClassLoader.loadClass() **

Class loading of class org.ph.gniithelp.training5.ClassA from ClassLoader 'sun.misc.Launcher$AppClassLoader@2a340e' in progress...
Creating a new instance of org.ph.gniithelp.training5.ClassA...
SUCCESS!: org.ph.gniithelp.training5.ClassA@6eb38a

Simulator done!


For the “baseline” run, the Java program is able to load ClassA successfully.

Now let’s voluntary change the full name of ClassA and re-run the program for each scenario. The following output can be observed:

#ClassA changed to ClassB
private static final String CLASS_TO_LOAD = "org.ph.gniithelp.training5.ClassB";


#Scenario 1 output (problem replication)
java.lang.ClassNotFoundException Simulator - Training 5
Author: Pierre-Hugues Charbonneau
http://gniithelp.blogspot.com

** Problem scenario #1: Class.forName() **

java.lang.ClassNotFoundException: org.ph.gniithelp.training5.ClassB
       at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
       at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
       at java.security.AccessController.doPrivileged(Native Method)
       at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:423)


This post first appeared on GNIITHELP, please read the originial post: here

Share the post

java.lang.ClassNotFoundException: How to resolve ~ gniithelp

×

Subscribe to Gniithelp

Get updates delivered right to your inbox!

Thank you for your subscription

×