How to Determine From Where a Class Is Loaded in Java


In this example we will show how to know from where a class is loaded in Java.

Source Code

package com.beginner.examples;

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

public class ClassPathExample {

    public static void main(String[] args){
    	try 
    	{
			Class example = Class.forName("com.beginner.examples.FileExample");		
			ProtectionDomain domain = example.getProtectionDomain(); 
			URL url = domain.getCodeSource().getLocation();
			System.out.println(url.getFile());
		} 
    	catch (ClassNotFoundException e) 
    	{
			e.printStackTrace();
		}
    }
}

Output:

/D:/Workspaces/cases/bin/

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments