Skip to content

[Native Image] Missing support for ServiceLoader services registered via module-info #9952

Closed
@JaroslavTulach

Description

@JaroslavTulach

Describe the Issue

Java Platform Module System defines a way to register services to be located by ServiceLoader. However there seems to be no support for such registrations in native image.

Using the latest version of GraalVM can resolve many issues.

GraalVM Version

I tried two versions:

  • GraalVM CE 21.0.2+13.1 (build 21.0.2+13-jvmci-23.1-b30)
  • Oracle GraalVM 23.0.1+11.1 (build 23.0.1+11-jvmci-b01)

None of them works the way it "should". Looking at the code of ServiceLoaderFeature explains why it is not working - there is no ModuleLayer related code at all - which is probably necessary to extract information from the module-info.class of the modules on module path.

Operating System and Version

Tested on Ubuntu, but this is cross OS problem

Diagnostic Flag Confirmation

  • I tried the -H:ThrowMissingRegistrationErrors= flag.

Run Command

I have prepared a special repository with NativeImageModuleDemo with a demo Maven project, follow the read me that suggests to:

NativeImageModularDemo$ mvn clean install
NativeImageModularDemo$ mvn -q -Pjvm -f api exec:exec
NativeImageModularDemo with following modules:
  Api
Found no API provider. Include Impl JPMS module!
NativeImageModularDemo$ mvn -q -Pjvm -f impl exec:exec
NativeImageModularDemo with following modules:
  Api
  Impl
Hi from Impl!

e.g. two modules Api and Impl are enabled and Hi from Impl! gets printed. While with native-image:

NativeImageModularDemo$ mvn clean install -Pnative
NativeImageModularDemo$ ./impl/target/NativeImageModularDemo
NativeImageModularDemo with following modules:
  Impl
  Api
Found no API provider. Fix native-image support for modular ServiceLoader!

e.g. two modules are enabled, but the ServiceLoader.load finds no implementation of the interface it is looking for.

Expected Behavior

Native image version should work just like the JVM one:

NativeImageModularDemo$ mvn clean install -Pnative
NativeImageModularDemo$ ./impl/target/NativeImageModularDemo
NativeImageModularDemo with following modules:
  Impl
  Api
Hi from Impl!

Actual Behavior

NativeImageModularDemo$ mvn clean install -Pnative
NativeImageModularDemo$ ./impl/target/NativeImageModularDemo
NativeImageModularDemo with following modules:
  Impl
  Api
Found no API provider. Fix native-image support for modular ServiceLoader!

Steps to Reproduce

I have prepared a special repository with NativeImageModuleDemo with a demo Maven project, follow the read me that suggests to:

NativeImageModularDemo$ mvn clean install
NativeImageModularDemo$ mvn -q -Pjvm -f api exec:exec
NativeImageModularDemo with following modules:
  Api
Found no API provider. Include Impl JPMS module!
NativeImageModularDemo$ mvn -q -Pjvm -f impl exec:exec
NativeImageModularDemo with following modules:
  Api
  Impl
Hi from Impl!

e.g. two modules Api and Impl are enabled and Hi from Impl! gets printed. While with native-image:

NativeImageModularDemo$ mvn clean install -Pnative
NativeImageModularDemo$ ./impl/target/NativeImageModularDemo
NativeImageModularDemo with following modules:
  Impl
  Api
Found no API provider. Fix native-image support for modular ServiceLoader!

e.g. two modules are enabled, but the ServiceLoader.load finds no implementation of the interface it is looking for.

Additional Context

No response

Run-Time Log Output and Error Messages

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions