JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

How To Load A Class Using Class.forName()

April 25, 2014 by Krishna Srinivasan Leave a Comment

This example shows how to load a class using the Class.forName() method. All the classes in your application or dependent class files are loaded by appropriate class loader prior to using in the application. By default, class files are loaded by JVM when first time accessed by the applications. However, in certain situations you have to explicitly load the class files to make it available for your application.

Class.forName() is mostly used for loading the classes. If you would have used the JDBC APIs, there we have loaded the database driver class file using the Class.forName() method. When you pass the class name, note that it has to be passed as the fully qualified path.

Lets look at the below example to understand how the class loader loads the class. If the class is not found by the class loader, JVM will throw java.lang.ClassNotFoundException.

ClassForNameExample.java

[code lang=”java”]
package javabeat.net.core;

/**
* Load class using Class.forName() Example
*
* @author Krishna
*
*/
public class ClassForNameExample {

public static void main(String[] args) {
try {
//Explicitly load class using forName()
Class.forName("javabeat.net.core.One");
} catch (ClassNotFoundException exception){
exception.printStackTrace();
}
try {
//Wrong way of specifying class name. It must be fully qualified path
Class.forName("Two");
} catch (ClassNotFoundException exception){
exception.printStackTrace();
}
try {
//Explicitly load class using forName()
Class.forName("javabeat.net.core.Two");
} catch (ClassNotFoundException exception){
exception.printStackTrace();
}

//Create instance
new Three();
}
}

class One{
static{
System.out.println("Class One Loaded");
}
}

class Two{
static{
System.out.println("Class Two Loaded");
}
}

class Three{
static{
System.out.println("Class Three Loaded");
}
}

[/code]

Output…

[code]
java.lang.ClassNotFoundException: Two
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at javabeat.net.core.ClassForNameExample.main(ClassForNameExample.java:14)
Class One Loaded
Class Two Loaded
Class Three Loaded
[/code]

Filed Under: Java Tagged With: Java Class

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved