Skip to content

overrideClassLoaders(AppClassLoader) not no-op with Java 11 #412

@chrisr3

Description

@chrisr3

This might just be a semantic issue, but it is an obvious change in behaviour vs running on Java 8.

We are executing ClassGraph as follows:

ClassGraph().overrideClassLoaders(classLoader)
    .ignoreParentClassLoaders()
    .enableClassinfo()
    .scan()

The classLoader is allowed to be AppClassLoader, and this isn't a problem on Java 8. However, it does cause problems on Java 11. I have tested ClassGraph 4.8.58, 4.8.65 and 4.8.67, and none of them finds any nonSystemModuleRefs, although 4.8.67 does at least find the systemModuleRefs. (To be precise, ClassGraph does find lots of "unnamed modules" on the call-stack, but rejects them all because layer == null.)

We are working around this by doing the following:

ClassGraph().apply {
    if (classLoader !== ClassLoader.getSystemClassLoader()) {
        overrideClassLoaders(classLoader)
    }
}.ignoreParentClassLoaders()
 .enableClassinfo()
 .scan()

This works because it allows ClasspathFinder to consult the java.class.path system property and so populate the classpathOrder field instead. And to be clear, these are the jars we want to be scanning. We have no need to scan the systemModuleRefs here.

See #382 and #411.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions