Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java : loop on all the classes in the classpath [duplicate]

Is there a way to iterate over all the classes in the classpath ?

I want to make some reflective checks on some classes implementing a certain interface, but I want to do it completely dynamically, without any input on which classes to check, just browsing the classpath.

like image 206
glmxndr Avatar asked Sep 30 '09 13:09

glmxndr


People also ask

How do I get all classes in a classpath?

You can get all classpath roots by passing an empty String into ClassLoader#getResources() . Enumeration<URL> roots = classLoader. getResources("");

How do I resolve a classpath problem?

You have 3 solutions: add this class in the path of your other compiled classes (respecting the package naming of your directories) add the root directory of this class in your classpath (in your case "C:\java\project\") add this single class into a jar and add this jar to the classpath.

How do I override a classpath class?

You can override the value of Classpath in Java defined by the environment variable CLASSPATH by providing the JVM command-line option –cp or –classpath while running your application.


1 Answers

The Reflections library helps deal with this problem. As others have stated, it isn't fully possible in all class loading situations, but if all you have are jars and files, this will do it reliably.

like image 132
Yishai Avatar answered Sep 19 '22 02:09

Yishai