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
  • Contact Us

How To Get Current Class Loader Name

April 24, 2014 by Krishna Srinivasan Leave a Comment

This example shows how to get the name of the class loader and parent class loader for the currently executing class. If you are not aware of the class loaders, please read our previous article about ClassLoader.

What is the ClassLoader ?

As its name implies, ClassLoader is a class that loads other classes. More scientific: The Classloader loads the bytecodes in its class to memory, so it can be used throughout your application. The ClassLoader is in the java.lang.ClassLoader package. Understanding the classloader is important for working on the server development or any product developments which is more often design the custom classloaders to load the classes. You would have come across the ClassNotFoundExecption many times while you are writing Java programming. Classloader is the culprit which causes this exception.

Lets look at the simple example for getting the current class loader and parent class loader.

GetClassLoaderExample.java

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

/**
* Get ClassLoader Example
*
* @author Krishna
*
*/
public class GetClassLoaderExample {

public static void main(String[] args) {
Class<?> classVar = GetClassLoaderExample.class;

//Print class loader name loaded this class
System.out.println("Current Class Loader : "
+ classVar.getClassLoader().getClass().getName());

//Print parent class loader name for the current class loader
System.out.println("Parent Class Loader : "
+ classVar.getClassLoader().getParent().getClass().getName());
}

}
[/code]

Output…

When you run the above code, you would get the output something like this. Note this will be depend on your Java environment.

[code]
Current Class Loader : sun.misc.Launcher$AppClassLoader
Parent Class Loader : sun.misc.Launcher$ExtClassLoader
[/code]

Filed Under: Java Tagged With: Java Classloader

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