Search code examples
javareflectionjarclassloader

How do I discover which classpath entry provided a class?


I am loading several different classes from several different .jars from a custom class loader in Java. I create a custom URLClassLoader, add several .jars to it and pass it on to a ServiceLoader to find the classes I want.

My question is: given a class, is there a way to discover which .jar it was loaded from?


Solution

  • You can call findResource on Classloader, and parse the URL that you get to figure out where it is coming from.