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 Check Where The Class Loaded From

March 17, 2014 by Krishna Srinivasan Leave a Comment

This example shows to check the path of the class loaded from. This utility would be helpful in certain cases if you want to check the class loading path when there is confusion over multiple libraries exist in the different path. To do that, the below example use the following steps:

  • Get the Class for the actual class to be checked for the path
  • Invoke the getProtectionDomain() nethod of the Class instance which returns the ProtectionDomain object which is originally hold the characteristics of the domian.
  • Then call the getCodeSource() method which returns the CodeSource object
  • Then call the getLocation() method of the CodeSource instance which returns the path of the class loaded.

Lets look at the example:

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

import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;

public class JavaCoreExample {
public static void main(String[] args) {
// get the runtime class of this Object
Class<?> cls = new JavaCoreExample().getClass();
ProtectionDomain pDomain = cls.getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
// get the location associated with this CodeSource
URL location = cSource.getLocation();
System.out.println("Location: " + location);
}
}
[/code]

Output

[code]
Location: file:/home/krishna/workspace/Java%20Project/bin/
[/code]

Filed Under: Java Tagged With: Core Java, Java Net

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