question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[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

See original GitHub issue

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);

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:20
  • Comments:10

github_iconTop GitHub Comments

6reactions
vlborkunovcommented, May 16, 2022

same problem, and we use java -jar command online…

i found another workaround:

var reflections = new Reflections(new ConfigurationBuilder()
                .setUrls(ClasspathHelper.forPackage(DeviceCode.class.getPackageName())));
var classes = reflections.getTypesAnnotatedWith(DeviceCode.class);
1reaction
olewehrmeyercommented, Dec 19, 2021

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

org.reflection.Reflections 0.10.2 fails when running as jar (e.g. ...
It works well running in IntelliJ and returns all implementations of MyService.class . But running in a docker container, it returns an empty ......
Read more >
org.reflection.Reflections 0.10.2 fails when running as jar (e.g. ...
Reflections 0.10.2 fails when running as jar (e.g. in a docker ... does not detect any classes, if base class (or package prefix)...
Read more >
Reflections (Reflections 0.10-RC2 API) - Javadoc.io
Reflections one-stop-shop object. Reflections scans and indexes your project's classpath, allowing reverse query of the type system metadata on runtime.
Read more >
Documentation - Apache Kafka
This tutorial assumes you are starting fresh and have no existing Kafka or ZooKeeper data. Since Kafka console scripts are different for Unix-based...
Read more >
Reflection - Kotlin
Functions and properties are first-class citizens in Kotlin, ... the runtime library for applications that do not use reflection features.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found