Which JAR is my Java Code Referencing?

02 Apr 2020

I saw a snippet of Java code in this Stack Overflow answer and I wanted to post it here:

Very useful if you need to check which jar is being accessed to load a specific class.

Java
1
2
3
4
ClassLoader classloader = fully.qualified.ClassName.class.getClassLoader();
java.net.URL res = classloader.getResource("fully/qualified/ClassName.class");
String path = res.getPath();
System.out.println("Package came from: " + path);