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

12 classic String-based Java interview questions with simple explanations and code.

  1️⃣ Check if a String is a Palindrome Problem Given a string, check if it reads the same forward and backward. Example: "madam...

Featured Posts