0

I was trying this sample code of reflection :

import java.lang.reflect.Method;

class Test 
{

  public Test() 
  { }

        public void sayHello() 
        {
             System.out.println("Hello");
        }
}

  public class Foo 
  {    

      public static void main (String[] args) throws Exception 
      {
          Class<?> clazz = Class.forName("Test");
          Method method = clazz.getMethod("sayHello");
          Object instance = clazz.newInstance();
          method.invoke(instance);

       }
   }

The following is the error that is been displayed :

 run:
    Exception in thread "main" java.lang.ClassNotFoundException: Test
     at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
     at java.security.AccessController.doPrivileged(Native Method)
     at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
     at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
     at java.lang.Class.forName0(Native Method)
     at java.lang.Class.forName(Class.java:169)
     at foo.Foo.main(Foo.java:29)
 Java Result: 1

How can, I solve the above error in the code. I also gave Foo.Test but it does not work at all.

Please help me resolve this. Thank you

Sagar Vyas
  • 19
  • 7
  • In which package is placed `Test` class you want to load? – Pshemo May 15 '14 at 13:12
  • If you have the class available on the classpath at both compile and run time you can use the class literal "clazz = Test.class", as its compile time checked (the compiler would already tell you if it can't find the class). classForName() is the way to go if the class is not always on the class path, or not known at compile time (e.g. JDBC driver) – Durandal May 15 '14 at 14:05

2 Answers2

2

You have to provide the fully-qualified name of the class (which includes the package and the name of the class). For example:

Class<?> clazz = Class.forName("foo.Test");
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • |Thank you, I was using Foo.Test and as PAT WANJAU said foo.Test worked. I had to use F in the lower case. Thank you anyways. – Sagar Vyas May 15 '14 at 13:25
2

From the Stack trace you've provided, it seems you have a package foo. In order for this to work try to replace your implementation with the following:

Class<?> clazz = Class.forName("foo.Test");

N.B. Always remember that for reflection to work, you will be required to provide the full class name including the package.

Patrick W
  • 1,485
  • 4
  • 19
  • 27
  • |Thank you, I was using Foo.Test as you said foo.Test worked. I had to use F in the lower case. Thank you so much . – Sagar Vyas May 15 '14 at 13:27
  • Dear Pat Wanju, You solved my error so gracefully, can you solve this question of mine . It would be of great help! as its my project and I need to get it done ASAP. [link]( http://stackoverflow.com/questions/23680503/how-to-reduce-teh-usage-of-if-else-by-reflection-can-i-get-the-code-example) – Sagar Vyas May 15 '14 at 14:03