Is something similar to ServiceLoader in Java 1.5?

How do I discover classes at runtime in the classpath which implements a defined interface?

ServiceLoader suits well (I think, I haven't used it), but I need do it in Java 1.5.

Asked by: Aston817 | Posted: 28-01-2022

Answer 1

There's nothing built into Java 1.5 for this. I implemented it myself; it's not too complicated. However, when we upgrade to Java 6, I will have to replace calls to my implementation with calls to ServiceLoader. I could have defined a little bridge between the app and the loader, but I only use it in a few places, and the wrapper itself would be a good candidate for a ServiceLoader.

This is the core idea:

public <S> Iterable<S> load(Class<S> ifc) throws Exception {
  ClassLoader ldr = Thread.currentThread().getContextClassLoader();
  Enumeration<URL> e = ldr.getResources("META-INF/services/" + ifc.getName());
  Collection<S> services = new ArrayList<S>();
  while (e.hasMoreElements()) {
    URL url = e.nextElement();
    InputStream is = url.openStream();
    try {
      BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8"));
      while (true) {
        String line = r.readLine();
        if (line == null)
        int comment = line.indexOf('#');
        if (comment >= 0)
          line = line.substring(0, comment);
        String name = line.trim();
        if (name.length() == 0)
        Class<?> clz = Class.forName(name, true, ldr);
        Class<? extends S> impl = clz.asSubclass(ifc);
        Constructor<? extends S> ctor = impl.getConstructor();
        S svc = ctor.newInstance();
    finally {
  return services;

Better exception handling is left as an exercise for the reader. Also, the method could be parameterized to accept a ClassLoader of the caller's choosing.

Answered by: Rebecca656 | Posted: 01-03-2022

Answer 2

javax.imageio.spi.ServiceRegistry is the equivalent with prior Java versions. It's available since Java 1.4.

It does not look like a general utility class, but it is. It's even a bit more powerful than ServiceLoader, as it allows some control over the order of the returned providers and direct access to the registry.


Answered by: Andrew275 | Posted: 01-03-2022

Answer 3

ServiceLoader is quite basic, and has been in use (informally) within the JDK since 1.3. ServiceLoader just finally made it a first class citizen. It simply looks for a resource file named for your interface, which is basically bundled in the META-INF directory of a library jar.

That file contains the name of the class to load.

So, you'd have a file named:


and inside it is a single line:

In lieu of ServiceLoader, I like Netbeans Lookup. It works with 1.5 (and maybe 1.4).

Out of the box, it does the exact same thing as ServiceLoader, and it's trivial to use. But it offers a lot more flexibility.

Here's a link:

Here's a article about ServiceLoader, but it mentions Netbeans Lookup at the bottom:

Answered by: Kelvin638 | Posted: 01-03-2022

Answer 4

This is an old question but the other option is to use Package Level Annotations. See my answer for: Find Java classes implementing an interface

Package level annotations are annotations that are in classes.

JAXB uses this instead of Service Loaders. I also think its more flexible than the service loader.

Answered by: Sydney655 | Posted: 01-03-2022

Answer 5


There's nothing built into Java 1.5 for this ...

is only a part of truth.

There is non-standard sun.misc.Service around.

Beware, it is not a part of standard J2SE API! It is non-standard part of Sun JDK. So you can not rely on it if you use, say, JRockit.

Answered by: Kelvin354 | Posted: 01-03-2022

Answer 6

There is no reliable way to know what classes are in the classpath. According to its documentation, ServiceLoader relies on external files to tell it what classes to load; you might want to do the same. The basic idea is to have a file with the name of the class(es) to load, and then use reflection to instantiate it/them.

Answered by: Elise115 | Posted: 01-03-2022

Similar questions

java - Has anyone used ServiceLoader together with Guice?

I keep wanting to try this on a larger scale with our app + build system, but higher priorities keep pushing it to the back burner. It seems like a nice way to load Guice modules and avoids the common complaint about "hard coded configuration". Individual configuration properties rarely change on their own, but you will almost always have a set of profiles, usually for different environments (Debug, Production, etc). ...

dependency injection - JAVA 6 ServiceLoader

I recently posted a question regarding a way to define the implementation of an abstract service on the client side. dfa mentioned java.util.Se...

java - ServiceLoader double iterator issues

Is this a known issue? I had trouble finding any search results. When iterating over a ServiceLoader while an iteration already is in progress, the first iteration will be aborted. For example, assuming there are at least two implementations of Foo, the following code will fail with an AssertionError: ServiceLoader&lt;Foo&gt; loader = ServiceLoader.load(Foo.class); Iterator&lt;Foo&gt; i...

How to make the java ServiceLoader work in a NetBeans 6.9 module application

I have troubles using the java ServiceLoader in a NetBeans module application. Here is what I'm trying to do (and it works in a normal java application in Eclipse): I have an interface.jar, which declares the interface. And I have implementations.jar, which has several implementations of this interface, all specified in the spi/META-INF/services/ file (this file is in the implemenations.ja...

What else can be used in Java as the ServiceLoader alternative?

I'm looking for something like the ServiceLoader, but which does not depend on SPI file, where all the service implementations should be enumerated and then added to the path of some class loader, in order to be found. Let's say there is an application, that has the interface and some implementations of a service. What framework can be used, that allows you to add a new JAR to the application, which contains some n...

java - Where to put ServiceLoader config file in a web app

I am writing a web app in Eclipse. I am trying to use the ServiceLoader class to load some plugins. The docs for ServiceLoader say I need to place a file in META-INF/services. I have placed the file in the WebContent/META-INF/service folder but when I run the JUnit test via Eclipse it does not find any plugins. Is this the correct location for the file? Also, how can I get more debug...

java - Using serviceloader on android

I am very new to java and android development and to learn I am trying to start with an application to gather statistics and information like munin does. I am trying to be able to load "plugins" in my application. These plugins are already in the application but I don't want to have to invoke them all separately, but be able to iterate over them. I was trying to use serviceloader but could never get the META-INF/services i...

Java ServiceLoader with multiple Classloaders

What are the best practices for using ServiceLoader in an Environment with multiple ClassLoaders? The documentation recommends to create and save a single service instance at initialization: private static ServiceLoader&lt;CodecSet&gt; codecSetLoader = ServiceLoader.load(CodecSet.class);

java - ServiceLoader where type parm is itself generic

class ServiceLoader&lt;S&gt; implements Iterable&lt;S&gt; { // ... } interface Foo&lt;T&gt; { // ... } class FooRepository { void add(Iterable&lt;Foo&lt;?&gt;&gt; foos) { // ... } } FooRepository repo = new FooRepository(); repo.add(ServiceLoader.load(Foo.class)); This yields a compiler error: "The method add(Iterable&lt;Foo&lt;?&gt;&gt;) in the type FooRepo...

java - ServiceLoader or another to dynamically load plugins

Read through Which describes the ServiceLoader. Fairly simple. People that use this API, how is polling the classpath or an application path for new updates usually done? Call the ServiceLoader.reload method every x minutes? Or do people use other open-source APIs? Or roll there own ServiceLoader like frames?

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)