I have a Java 17 application which uses RandomGenerator.getDefault()
in it. When I use jdeps to create a minified JRE for it, it does not add jdk.random
, so the JRE created by jlink cannot run the application. Is there something I missed?
CodePudding user response:
The RandomGenerator
interface is in the module java.base
, which can not depend on any other module, including jdk.random
.
Instead it uses the ServiceLoader
API to find implementations of RandomGenerator
. The jdk.random
module provides several implementations of the RandomGenerator
interface:
> java --describe-module jdk.random
jdk.random@19
...
provides java.util.random.RandomGenerator with jdk.random.L32X64MixRandom jdk.random.L64X128MixRandom jdk.random.L64X128StarStarRandom jdk.random.L64X256MixRandom jdk.random.L64X1024MixRandom jdk.random.L128X128MixRandom jdk.random.L128X256MixRandom jdk.random.L128X1024MixRandom jdk.random.Xoroshiro128PlusPlus jdk.random.Xoshiro256PlusPlus
...
However, since there is no explicit dependency from java.base
to jdk.random
, it is not included automatically when you include java.base
.
Modules that implement services have to be included manually using --add-modules
when running jlink
instead.
You can use jlink
's --suggest-providers
option to get a list of modules that implement a service:
> jlink --suggest-providers java.util.random.RandomGenerator
Suggested providers:
java.base provides java.util.random.RandomGenerator used by java.base
jdk.random provides java.util.random.RandomGenerator used by java.base
(There's also the --bind-services
flag, but that will include ALL service implementations, which is probably not what you want)