Create a trivial jar (let's say foo.jar) which just contains a single META-INF/services/java.util.random.RandomGenerator file whose contents are:
foo.DummyRandomGenerator
Now run the following Main application with that jar in the classpath. The main() method just calls RandomGeneratorFactory.all() method:
import java.util.random.RandomGeneratorFactory;
public class Main {
public static void main(String[] args) {
System.out.println("available RandomGenerator(s):");
RandomGeneratorFactory.all().map(RandomGeneratorFactory::name).forEach(System.out::println);
}
}
javac Main.java
java -cp .:foo.jar Main
This leads to the following exception:
Exception in thread "main" java.util.ServiceConfigurationError: java.util.random.RandomGenerator: Provider foo.DummyRandomGenerator not found
at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:589)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.nextProviderClass(ServiceLoader.java:1215)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1224)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1269)
at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1305)
at java.base/java.util.ServiceLoader$ProviderSpliterator.tryAdvance(ServiceLoader.java:1485)
at java.base/java.util.Spliterator.forEachRemaining(Spliterator.java:332)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:556)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:546)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:265)
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:702)
at java.base/java.util.random.RandomGeneratorFactory$FactoryMapHolder.createFactoryMap(RandomGeneratorFactory.java:149)
at java.base/java.util.random.RandomGeneratorFactory$FactoryMapHolder.<clinit>(RandomGeneratorFactory.java:137)
at java.base/java.util.random.RandomGeneratorFactory.getFactoryMap(RandomGeneratorFactory.java:168)
at java.base/java.util.random.RandomGeneratorFactory.all(RandomGeneratorFactory.java:388)
at Main.main(Main.java:6)
foo.DummyRandomGenerator
Now run the following Main application with that jar in the classpath. The main() method just calls RandomGeneratorFactory.all() method:
import java.util.random.RandomGeneratorFactory;
public class Main {
public static void main(String[] args) {
System.out.println("available RandomGenerator(s):");
RandomGeneratorFactory.all().map(RandomGeneratorFactory::name).forEach(System.out::println);
}
}
javac Main.java
java -cp .:foo.jar Main
This leads to the following exception:
Exception in thread "main" java.util.ServiceConfigurationError: java.util.random.RandomGenerator: Provider foo.DummyRandomGenerator not found
at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:589)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.nextProviderClass(ServiceLoader.java:1215)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1224)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1269)
at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1305)
at java.base/java.util.ServiceLoader$ProviderSpliterator.tryAdvance(ServiceLoader.java:1485)
at java.base/java.util.Spliterator.forEachRemaining(Spliterator.java:332)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:556)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:546)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:265)
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:702)
at java.base/java.util.random.RandomGeneratorFactory$FactoryMapHolder.createFactoryMap(RandomGeneratorFactory.java:149)
at java.base/java.util.random.RandomGeneratorFactory$FactoryMapHolder.<clinit>(RandomGeneratorFactory.java:137)
at java.base/java.util.random.RandomGeneratorFactory.getFactoryMap(RandomGeneratorFactory.java:168)
at java.base/java.util.random.RandomGeneratorFactory.all(RandomGeneratorFactory.java:388)
at Main.main(Main.java:6)
- relates to
-
JDK-8332086 Remove the usage of ServiceLoader in j.u.r.RandomGeneratorFactory
- Resolved