8

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);
          }
       }
    }
2
  • 3
    The previous answers are correct, nothing to add except that your package names should be all lowercase. It's the Java convention, and sticking to conventions makes the coding world a little brighter. Commented Jan 6, 2012 at 12:25
  • Thanks pcalcao...I will keep in mind Commented Jan 6, 2012 at 12:31

2 Answers 2

18

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

Sign up to request clarification or add additional context in comments.

1 Comment

Oh dat was too fast....Thanks Jon.Yes Jon I know package name is pretty odd but I am learning Java reflection now thats why I have just taken any package name
6

You need Class.forName("com.Test.A") instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.