Proper way to scan all classes inside application/war files
Alan Bateman
Alan.Bateman at oracle.com
Fri Apr 19 07:48:30 UTC 2019
On 19/04/2019 01:33, Sundara Mohan M wrote:
> Hi,
> I was scanning all classes (to find all annotated class) using the
> URLClassLoader.getUrls() methods to find all URL and find it with JDK8.
> Since JDK9 onwards all App/System Class loaders are not deriving from
> URLClassLoader it doesn't work anymore. (solution of making ucp variable
> inside BuiltinClassLoader accesible and reading URLs works).
I think you are looking for the "java.class.path" system property. Split
the value, map each element to a file path and file URL, and you should
the equivalent to the array of URLs that getURLs would have given you.
Same limitations too in that it's just the initial class path and
doesn't taken into account additions that arise when JAR files have the
Class-Path attribute.
>
> What is the proper way to scan all(AppClassLoader + any other class loader)
> classes?
>
This is a bigger question. I suspect you are looking to enumerate all
the resources in all modules in the boot layer. The following will print
the names of all resources in all modules in the boot layer and might
give you ideas:
ModuleLayer.boot().configuration().modules().forEach(m -> {
System.out.format("module %s%n", m.name());
try (ModuleReader reader = m.reference().open()) {
reader.list().forEach(rn -> System.out.format("
%s%n", rn));
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
});
In addition you want the resources on the class path and I think you
have code for this already once you map the value of java.class.path to
a sequence of file URLs.
-Alan
More information about the jigsaw-dev
mailing list