How to scan JARs loaded at runtime using the Google reflection library?

Is there a solution for setting up the reflection library so that it also looks at JARs that are added at runtime using the URLClassLoader?

Now reflections just look at the URLs in ClassLoader. This is the configuration I'm using now:

Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader())); 

I could not find any hints in the reflection library document.

EDIT: This is how I upload the jar file:

 File f = new File("C:/Users/mkorsch/Desktop/test-reflections.jar"); URLClassLoader urlCl = new URLClassLoader(new URL[] {f.toURI().toURL()},System.class.getClassLoader()); 
+4
source share
1 answer

You may also need to use the appropriate class loader,

 Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage("my.package", myClassLoader)).addClassLoader(myClassLoader)); 

Or simply:

 new Reflections("my.package", myClassLoader, scanners, ...) 

Check this out in the documentation .

+4
source

Source: https://habr.com/ru/post/1490461/


All Articles