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);
}
}
}
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.
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.
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.
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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With