Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacement class for sun.misc.Service

Tags:

java

java-9

I have some old (pre java 6) code that depends on the sun.misc.Service API, such as:

sun.misc.Service.providers(MyServiceProvider.class);

This class is being removed (not just internalized) in Java 9.

What alternate Java SE API can I use?

like image 834
Andy Guibert Avatar asked Mar 10 '23 18:03

Andy Guibert


1 Answers

The sun.misc.Service class has been replaced by java.util.ServiceLoader in Java 6.

The following code examples are equivalent:

Before

Iterator<MyServiceProvider> = sun.misc.Service.providers(MyServiceProvider.class);

After

Iterator<MyServiceProvider> = java.util.ServiceLoader.load(MyServiceProvider.class).iterator();

As those in comments have pointed out, java.util.ServiceLoader has been around for a long time (Java 6).

like image 69
Andy Guibert Avatar answered Mar 21 '23 03:03

Andy Guibert