class

Check where a class was loaded from

This is an example of how to check where a class was loaded from. To check where a class was loaded from we have created a class, CheckWhereAClassWasLoadedFrom. It has a method void getLocation() that gets the location of the class. The steps of the method are described below:

  • It gets the runtime class of this Object using getClass() API method of Object for the specific instance.
  • It gets the ProtectionDomain that encapsulates the characteristics of a domain, which encloses a set of classes whose instances are granted a set of permissions when being executed on behalf of a given set of Principals, using getProtectionDomain() API method of Class.
  • It gets the CodeSource to encapsulate not only the location (URL) but also the certificate chains that were used to verify signed code originating from that location, with getCodeSource() API method of ProtectionDomain.
  • Then it gets the URL associated with this CodeSource, with getLocation() API method of CodeSource.

Let’s take a look at the code snippet that follows:  

package com.javacodegeeks.snippets.core;

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

public class CheckWhereAClassWasLoadedFrom {
	
	public static void main(String[] args) {
		new CheckWhereAClassWasLoadedFrom().getLocation();
	}
	
	private void getLocation() {
		
		// get the runtime class of this Object
		Class<?> cls = this.getClass();
		
		// get the ProtectionDomain, a class that encapsulates the characteristics of a domain,
		// which encloses a set of classes whose instances are granted a set 
		// of permissions when being executed on behalf of a given set of Principals
		ProtectionDomain pDomain = cls.getProtectionDomain();
		
		// get the CodeSource, a class extends the concept of a codebase to
		// encapsulate not only the location (URL) but also the certificate chains 
		// that were used to verify signed code originating from that location
		CodeSource cSource = pDomain.getCodeSource();
		
		// get the location associated with this CodeSource
		URL location = cSource.getLocation();
		
		System.out.println("Location: " + location);
		
	}

}

Output:

Location: file:/C:/workspace/MyProject/bin/

  
This was an example of how to check where a class was loaded from in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button