Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class.forName() throws ClassNotFoundException

Tags:

java

A Thinking in Java program is as follows:

package typeinfo;
import static util.Print.*;

class Candy {
 static { print("Loading Candy"); }
}

class Gum {
 static { print("Loading Gum"); }
}

class Cookie {
 static { print("Loading Cookie"); }
}

public class SweetShop {
 public static void main(String[] args) {  
   print("inside main");
   new Candy();
   print("After creating Candy");
   try {
     Class.forName("Gum");
   } catch(ClassNotFoundException e) {
     print("Couldn't find Gum");
   }
   print("After Class.forName(\"Gum\")");
   new Cookie();
   print("After creating Cookie");
 }
} 

I am expecting the output as follows:

/* Output:
inside main
Loading Candy
After creating Candy
Loading Gum
After Class.forName("Gum")
Loading Cookie
After creating Cookie
*/

But get

inside main
Loading Candy
After creating Candy
Couldn't find Gum
After Class.forName("Gum")
Loading Cookie
After creating Cookie

Obviously the try block is throwing a ClassNotFoundException, which is unexpected. Any ideas why the code throws this instead of initializing the Gum class, as expected?

like image 344
Terry Avatar asked Mar 14 '13 18:03

Terry


People also ask

What the class forName () does?

forName. Returns the Class object associated with the class or interface with the given string name, using the given class loader. Given the fully qualified name for a class or interface (in the same format returned by getName ) this method attempts to locate, load, and link the class or interface.

Why do we get ClassNotFoundException?

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.

What is ClassNotFoundException in Java example?

ClassNotFoundException occurs when you try to load a class at runtime using Class. forName() or loadClass() methods and requested classes are not found in classpath. Most of the time this exception will occur when you try to run an application without updating the classpath with JAR files.

What is the difference between NoClassDefFoundError and ClassNotFoundException?

Both ClassNotFoundException and NoClassDefFoundError are the errors when JVM or ClassLoader not able to find appropriate class while loading at run-time. ClassNotFoundException is a checked exception and NoClassDefFoundError is an Error which comes under unchecked.


1 Answers

Your classes are in the package typeinfo, so their fully-qualified names are typeinfo.Gum, typeinfo.Candy and typeinfo.Cookie. Class.forName() only accepts fully-qualified names:

Parameters:

className - the fully qualified name of the desired class.

Change your code to:

try {
  Class.forName("typeinfo.Gum");
} catch(ClassNotFoundException e) {
  print("Couldn't find Gum");
}
like image 104
Jon Skeet Avatar answered Oct 10 '22 03:10

Jon Skeet