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.
You can get all classpath roots by passing an empty String into ClassLoader#getResources() . Enumeration<URL> roots = classLoader. getResources("");
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With