Accepted answer

create a new source folder with the location src/main/resources, then create your meta-inf/services folder in there and drop in your fully-qualified class name (fqcn) file. this should copy them into the jar file automatically. so for implementations of an interface with a fqcn of com.acme.myinterface, you'll have:

| src
| | main
|   | java
|     | [your source code]
|   | resources
|     | meta-inf
|       | services
|         | com.acme.myinterface

note that com.acme.myinterface is the name of the file, not a directory structure like a java package. the name of the file is the fqcn of the interface that you're implementing, and in it, you'll have the fqcn of each implementation on its own line, e.g.:


it's worth noting that this applies to gradle projects with the default source sets as well.

once you do this, you can load all the implementations of the interface using serviceloader:

serviceloader<myinterface> loader = serviceloader.load(myinterface.class);
for (myinterface service : loader) {
    // prints com.example.myinterfaceimpl and com.example.anothermyinterfaceimpl

some things to note:

  • all the implementations must have a no-args constructor
  • applications that use modules or custom classloaders may have to use the overloads of serviceloader.load

if these conditions won't work for you, then you may want to switch to another system, e.g. a cdi-style framework like spring, ejb, etc.


alternatively, if your project does not use a standard directory structure (or your simply wish to have alternate resource directories), you can specify resource directories manually the pom file.

for example, if your meta-inf/services is located in a folder called resources which lies in the root of your project directory, could specify it as follows:


you can use this to specify several directories by adding multiple <resource> elements.


by default maven looks for resources at:


so put it at


Related Query

More Query from same tag