ClassPathScanningCandidateComponentProvider works and it can scan all classes. But, it does not work when the code compiled to native.

code:

public static List<Class<?>> scanPackage(String packageName, TypeFilter includeFilter) {
        ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
        provider.addIncludeFilter(includeFilter);
        List<Class<?>> classes = new ArrayList<>();
        Set<BeanDefinition> beanDefinitions = provider.findCandidateComponents(packageName);
        log.debug("Scanner classes:{}", beanDefinitions.stream().map(BeanDefinition::getBeanClassName).toList());
        for (BeanDefinition candidate : beanDefinitions) {
            String beanClassName = candidate.getBeanClassName();
            try {
                classes.add(Class.forName(beanClassName));
            } catch (ClassNotFoundException e) {
                log.error("scanPackage exception ", e);
                throw new RuntimeException(e);
            }
        }
        return classes;
    }

The hints:

hints.reflection()
                    .registerType(ClassPathScanningCandidateComponentProvider.class, MemberCategory.values())
                    .registerType(BeanDefinition.class, MemberCategory.values())
                    .registerType(Class.class, MemberCategory.values())

my app. jar started by JVM,I can get a list with all classess. but started in native-image mode, the code returns an empty list. I'm resolving the problem, but failed.

Comment From: bclozel

This is the expected behavior in a native image:

For instance, classpath scanning does not work in a native image as there is no notion of a classpath. For cases like this, it is crucial that the scanning happens at build time.

Please look into this section of the reference documentation to better understand how such cases can be implemented with AOT.