How to get class loaded location in Java

Example

package com.pretech;
import java.security.CodeSource;
public class ClassLoadLocation {
	public static void main(String[] args) {
		Class<ClassLoadLocation> clazz = ClassLoadLocation.class;
		CodeSource codeSource = clazz.getProtectionDomain().getCodeSource();
		System.out.println(clazz.getName()+ " class is loaded from : " + codeSource.getLocation());
	}
}

Output



com.pretech.ClassLoadLocation class is loaded from : file:/D:/Projects/stsworkspace/core-java/target/classes/


No comments:

Post a Comment

Model Context Protocol (MCP) — Complete Guide for Backend Engineers

  Model Context Protocol (MCP) — Complete Guide for Backend Engineers Build Tools, Resources, and AI-Driven Services Using LangChain Moder...

Featured Posts