Up to JDK8, I could use the following to iterate on rt.jar classes. Given a single class, I could find all others like this :
final URL location = clazz.getProtectionDomain().getCodeSource().getLocation();
final File file = new File(location.toURI());
try (JarFile jarFile = new JarFile(file)) {
final Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
final JarEntry jarEntry = entries.nextElement();
// do something...
}
}
After JDK8, using this clazz.getProtectionDomain().getCodeSource().getLocation()
isn't valid anymore:
java.lang.NullPointerException: Cannot invoke "java.security.CodeSource.getLocation()" because the return value of "java.security.ProtectionDomain.getCodeSource()" is null
Is there good replacement for this ? I'm thinking of doing a special case like this:
if (clazz.getProtectionDomain().getCodeSource() == null) {
// find URL to the jmod ...
}
However a solution that will work in both cases would be preferable.