• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • 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)
  • 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)

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]

Category: JavaTag: 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.

Previous Post: « Get Methods Return Type using Reflection
Next Post: How To Find A File In Classpath »

Reader Interactions

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.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact