Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Nested Classes](https://developer.android.com/reference/java/util/ServiceLoader#nestedclasses) \| [Methods](https://developer.android.com/reference/java/util/ServiceLoader#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/java/util/ServiceLoader#inhmethods)

# ServiceLoader

` public final class ServiceLoader `   
` extends https://developer.android.com/reference/java/lang/Object ` ` implements https://developer.android.com/reference/java/lang/Iterable<S> `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | java.util.ServiceLoader\<S\> |

<br />

*** ** * ** ***

A facility to load implementations of a service.

A *service* is a well-known interface or class for which zero, one, or many service providers exist. A *service provider* (or just *provider*) is a class that implements or subclasses the well-known interface or class. A `ServiceLoader` is an object that locates and loads service providers deployed in the run time environment at a time of an application's choosing. Application code refers only to the service, not to service providers, and is assumed to be capable of choosing between multiple service providers (based on the functionality they expose through the service), and handling the possibility that no service providers are located.

## Obtaining a service loader

An application obtains a service loader for a given service by invoking one of the static `load` methods of `ServiceLoader`.

| ### Nested classes ||
|---|---|
| ` interface` | `https://developer.android.com/reference/java/util/ServiceLoader.Provider<S>` Represents a service provider located by `ServiceLoader`. |

| ### Public methods ||
|---|---|
| ` https://developer.android.com/reference/java/util/Optional<S>` | ` https://developer.android.com/reference/java/util/ServiceLoader#findFirst()() ` Load the first available service provider of this loader's service. |
| ` https://developer.android.com/reference/java/util/Iterator<S>` | ` https://developer.android.com/reference/java/util/ServiceLoader#iterator()() ` Returns an iterator to lazily load and instantiate the available providers of this loader's service. |
| ` static <S> https://developer.android.com/reference/java/util/ServiceLoader<S>` | ` https://developer.android.com/reference/java/util/ServiceLoader#load(java.lang.Class<S>)(https://developer.android.com/reference/java/lang/Class<S> service) ` Creates a new service loader for the given service type, using the current thread's [context class loader](https://developer.android.com/reference/java/lang/Thread#getContextClassLoader()). |
| ` static <S> https://developer.android.com/reference/java/util/ServiceLoader<S>` | ` https://developer.android.com/reference/java/util/ServiceLoader#load(java.lang.Class<S>,%20java.lang.ClassLoader)(https://developer.android.com/reference/java/lang/Class<S> service, https://developer.android.com/reference/java/lang/ClassLoader loader) ` Creates a new service loader for the given service. |
| ` static <S> https://developer.android.com/reference/java/util/ServiceLoader<S>` | ` https://developer.android.com/reference/java/util/ServiceLoader#loadInstalled(java.lang.Class<S>)(https://developer.android.com/reference/java/lang/Class<S> service) ` Creates a new service loader for the given service type, using the extension class loader. |
| ` void` | ` https://developer.android.com/reference/java/util/ServiceLoader#reload()() ` Clear this loader's provider cache so that all providers will be reloaded. |
| ` https://developer.android.com/reference/java/util/stream/Stream<https://developer.android.com/reference/java/util/ServiceLoader.Provider<S>>` | ` https://developer.android.com/reference/java/util/ServiceLoader#stream()() ` Returns a stream to lazily load available providers of this loader's service. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/util/ServiceLoader#toString()() ` Returns a string describing this service. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.
From interface ` https://developer.android.com/reference/java/lang/Iterable ` ` default void` ` https://developer.android.com/reference/java/lang/Iterable#forEach(java.util.function.Consumer<?%20super%20T>)(https://developer.android.com/reference/java/util/function/Consumer<? super T> action) `

Performs the given action for each element of the `Iterable` until all elements have been processed or the action throws an exception.
` abstract https://developer.android.com/reference/java/util/Iterator<S>` ` https://developer.android.com/reference/java/lang/Iterable#iterator()() `

Returns an iterator over elements of type `T`.
` default https://developer.android.com/reference/java/util/Spliterator<S>` ` https://developer.android.com/reference/java/lang/Iterable#spliterator()() `

Creates a `https://developer.android.com/reference/java/util/Spliterator` over the elements described by this `Iterable`.

## Public methods

### findFirst

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Optional<S> findFirst ()
```

Load the first available service provider of this loader's service. This convenience method is equivalent to invoking the `https://developer.android.com/reference/java/util/ServiceLoader#iterator()` method and obtaining the first element. It therefore returns the first element from the provider cache if possible, it otherwise attempts to load and instantiate the first provider.

The following example loads the first available service provider. If no service providers are located then it uses a default implementation.

    CodecFactory factory = ServiceLoader.load(CodecFactory.class)
                                           .findFirst()
                                           .orElse(DEFAULT_CODECSET_FACTORY);

<br />

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/Optional<S>` | The first service provider or empty `Optional` if no service providers are located <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/util/ServiceConfigurationError` | If a provider class cannot be loaded for any of the reasons specified in the [Errors](https://developer.android.com/reference/java/util/ServiceLoader#errors) section above. |

### iterator

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Iterator<S> iterator ()
```

Returns an iterator to lazily load and instantiate the available providers of this loader's service.

To achieve laziness the actual work of locating and instantiating providers is done by the iterator itself. Its `https://developer.android.com/reference/java/util/Iterator#hasNext()` and `https://developer.android.com/reference/java/util/Iterator#next()` methods can therefore throw a `https://developer.android.com/reference/java/util/ServiceConfigurationError` for any of the reasons specified in the [Errors](https://developer.android.com/reference/java/util/ServiceLoader#errors) section above. To write robust code it is only necessary to catch `ServiceConfigurationError` when using the iterator. If an error is thrown then subsequent invocations of the iterator will make a best effort to locate and instantiate the next available provider, but in general such recovery cannot be guaranteed.

Caching: The iterator returned by this method first yields all of the elements of the provider cache, in the order that they were loaded. It then lazily loads and instantiates any remaining service providers, adding each one to the cache in turn. If this loader's provider caches are cleared by invoking the `https://developer.android.com/reference/java/util/ServiceLoader#reload()` method then existing iterators for this service loader should be discarded. The `hasNext` and `next` methods of the iterator throw `https://developer.android.com/reference/java/util/ConcurrentModificationException` if used after the provider cache has been cleared.

The iterator returned by this method does not support removal. Invoking its `https://developer.android.com/reference/java/util/Iterator#remove()` method will cause an `https://developer.android.com/reference/java/lang/UnsupportedOperationException` to be thrown.

<br />

##### API Note:

- Throwing an error in these cases may seem extreme. The rationale for this behavior is that a malformed provider-configuration file, like a malformed class file, indicates a serious problem with the way the Java virtual machine is configured or is being used. As such it is preferable to throw an error rather than try to recover or, even worse, fail silently.

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/Iterator<S>` | An iterator that lazily loads providers for this loader's service <br /> |

### load

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static ServiceLoader<S> load (Class<S> service)
```

Creates a new service loader for the given service type, using the current thread's [context class loader](https://developer.android.com/reference/java/lang/Thread#getContextClassLoader()).

An invocation of this convenience method of the form

    ServiceLoader.load(service)

is equivalent to

    ServiceLoader.load(service, Thread.currentThread().getContextClassLoader())

<br />

<br />

##### API Note:

- Service loader objects obtained with this method should not be cached VM-wide. For example, different applications in the same VM may have different thread context class loaders. A lookup by one application may locate a service provider that is only visible via its thread context class loader and so is not suitable to be located by the other application. Memory leaks can also arise. A thread local may be suited to some applications.

| Parameters ||
|---|---|
| `service` | `Class`: The interface or abstract class representing the service <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/ServiceLoader<S>` | A new service loader <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/util/ServiceConfigurationError` | if the service type is not accessible to the caller |

### load

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static ServiceLoader<S> load (Class<S> service, 
                ClassLoader loader)
```

Creates a new service loader for the given service. The service loader uses the given class loader as the starting point to locate service providers for the service.

<br />

##### API Note:

- If the class path of the class loader includes remote network URLs then those URLs may be dereferenced in the process of searching for provider-configuration files. This activity is normal, although it may cause puzzling entries to be created in web-server logs. If a web server is not configured correctly, however, then this activity may cause the provider-loading algorithm to fail spuriously.

  A web server should return an HTTP 404 (Not Found) response when a requested resource does not exist. Sometimes, however, web servers are erroneously configured to return an HTTP 200 (OK) response along with a helpful HTML error page in such cases. This will cause a `https://developer.android.com/reference/java/util/ServiceConfigurationError` to be thrown when this class attempts to parse the HTML page as a provider-configuration file. The best solution to this problem is to fix the misconfigured web server to return the correct response code (HTTP 404) along with the HTML error page.

| Parameters ||
|---|---|
| `service` | `Class`: The interface or abstract class representing the service <br /> |
| `loader` | `ClassLoader`: The class loader to be used to load provider-configuration files and provider classes, or `null` if the system class loader (or, failing that, the bootstrap class loader) is to be used <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/ServiceLoader<S>` | A new service loader <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/util/ServiceConfigurationError` | if the service type is not accessible to the caller |