reflections: [0.10.2] Reflections does not detect any classes, if base class (or package prefix) is passed as argument, and application is running as a jar
Given the application built using Java 11 and Spring Boot framework, interface DeviceCode, residing inside the some.package package, and enum classes some.package.a.A, some.package.b.B, some.package.c.C, each implementing the DeviceCode: and code:
public interface DeviceCode {
static List<DeviceCode> getDeviceCodes() {
Reflections reflections = new Reflections(DeviceCode.class);
Set<Class<? extends DeviceCode>> classes = reflections.getSubTypesOf(DeviceCode.class);
// ...
}
}
// SomeService.java that runs the code above on start
@Service
class SomeService {
private final List<DeviceCode> devices = DeviceCode.getDeviceCodes();
// ...
}
Expected behaviour:
After migrating from 0.9.12, reflections should detect all the classes implementing DeviceCode interface, in this case A, B, C, as usual, no matter the environment that the application runs on.
Actual behaviour:
Although the code runs as expected, when ran using the IDE (e.g. IntelliJ IDEA), or build tools (during tests) like Maven, no class is detected when the application is launched using java -jar command. Same issue appears, if we initiate reflections using interface’s package as an argument:
Reflections reflections = new Reflections("some.package"); // or new Reflections (DeviceCode.class.getPackageName());
Current workaround:
Reflections object must be initiated using ConfigurationBuilder instance, in order to restore the previous functionality, when application is running as a jar.
Reflections reflections =
new Reflections(new ConfigurationBuilder().forPackages(DeviceCode.class.getPackageName()));
Set<Class<? extends DeviceCode>> classes = reflections.getSubTypesOf(DeviceCode.class);
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 34
- Comments: 16
Commits related to this issue
- :bug: Workaround for ronmamo/reflections#373 — committed to devkanro/sisyphus by devkanro 3 years ago
- :bug: Workaround for ronmamo/reflections#373 (#402) — committed to ButterCam/sisyphus by devkanro 3 years ago
- Fix reflection has not found any command classes. Solution is mentioned at: https://github.com/ronmamo/reflections/issues/373 — committed to vn-vna/EriPrincess by vn-vna 2 years ago
- 2nd try to fix reflection has not found any command classes. Solution is mentioned at: https://github.com/ronmamo/reflections/issues/373#issuecomment-1128077469 — committed to vn-vna/EriPrincess by vn-vna 2 years ago
- <fix>[utils]: use ConfigurationBuilder to init reflection scan Use ConfigurationBuilder instead of use contructor directly and filter packages do not start with 'org.zstack' See details: https://gi... — committed to zstackio/zstack by AlanJager 2 years ago
- fix(rest): fix bug with reflections not finding crud controllers Issue is outlined here https://github.com/ronmamo/reflections/issues/373 — committed to krud-dev/crudframework by Idane 2 years ago
- :bug: Revert to reflections 0.9.12 because of https://github.com/ronmamo/reflections/issues/373 — committed to reactome/reactome-parent by EliotRagueneau a year ago
- Apply workaround for package detection with reflections See https://github.com/ronmamo/reflections/issues/373#issuecomment-1828227344 — committed to Onto-Med/top-backend by ChristophB 7 months ago
- Apply workaround for package detection with reflections See https://github.com/ronmamo/reflections/issues/373#issuecomment-1828227344 — committed to Onto-Med/top-backend by ChristophB 7 months ago
- Apply workaround for package detection with reflections See https://github.com/ronmamo/reflections/issues/373#issuecomment-1828227344 — committed to Onto-Med/top-backend by ChristophB 7 months ago
- Apply workaround for package detection with reflections See https://github.com/ronmamo/reflections/issues/373#issuecomment-1828227344 — committed to Onto-Med/top-backend by ChristophB 7 months ago
- bugfix: use ConfigurationBuilder to fix reflection on Spring Boot Problem detected when using the task `bootJar` from Spring Boot applications See details: https://github.com/ronmamo/reflections/issu... — committed to Softawii/curupira by FerroEduardo 6 months ago
i found another workaround:
The workaround mentioned above does not work for JAR files created with Spring Boot. JAR URL is then
and it internally throws an exception that
/BOOT-INF/classes!/cannot be opened.Can confirm, had the exact same issue. Worked when started “outside” of a JAR, failed when inside when using
new Reflections("my.pkg.name.here").The provided workaround worked like a charm, thanks a lot!