Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class not found exception in Java Reflection

Hi I am using the following code from this site: http://java.sun.com/developer/technicalArticles/ALT/Reflection/

But when I am running it it showing exception java.lang.ClassNotFoundException: A May be I am going somewhere wrong Please help. Here is the code:

package com.Test;  
  class A {}

public class instance1 {
       public static void main(String args[])
       {
          try {
             Class cls = Class.forName("A");
            System.out.println("gfsdga");
             boolean b1 
               = cls.isInstance(new Integer(37));
             System.out.println(b1);
             boolean b2 = cls.isInstance(new A());
             System.out.println(b2);
          }
          catch (Throwable e) {
             System.err.println(e);
          }
       }
    }
like image 852
Maverick Avatar asked Jan 06 '12 12:01

Maverick


People also ask

What causes class not found exception in Java?

ClassNotFoundException is a checked exception in Java that occurs when the JVM tries to load a particular class but does not find it in the classpath.

How do I fix class not found exception?

When you get a ClassNotFoundException, it means the JVM has traversed the entire classpath and not found the class you've attempted to reference. The solution, as so often in the Java world, is to check your classpath. You define a classpath on the command line by saying java -cp and then your classpath.

What causes class not found exception?

ClassNotFoundException is a checked exception which occurs when an application tries to load a class through its fully-qualified name and can not find its definition on the classpath. This occurs mainly when trying to load classes using Class.

How do I fix Java Lang ClassNotFoundException in eclipse?

click on project->properties->Java build path->Source and check each src folder is still valid exist or recently removed. Correct any missing path or incorrect path and rebuild and run the test. It will fix the problem.


1 Answers

The class is actually called com.Test.A because you've declared it within the com.Test package - Class.forName() takes the package-qualified class name. (Note that com.Test is a pretty odd package name, too.)

like image 117
Jon Skeet Avatar answered Oct 25 '22 20:10

Jon Skeet