Where to put ServiceLoader config file in a web app
Asked Answered
H

1

8

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 info from ServiceLoader such as the folders it is searching for the file in?

Hemihydrate answered 6/8, 2010 at 2:51 Comment(0)
K
10

The META-INF/services should be at the root of the jar file or resource directory in the classpath of the classloader used to locate plugins.

If you have a web application, then you can have directory structure like:

<webappname>/WEB-INF/classes/META-INF/services

But the ideal way is to have it in your plugin's jar file. E.g if you have a plugin bundled as WEB-INF/lib/myplugin.jar, and your plugin class is com.example.plugin.MyPlugin Then the jar should have a structure:

myplugin.jar!/META-INF/services/com.example.plugin.MyPlugin

Having done that, you can get a service loader using your web application's class loader

Kuchen answered 6/8, 2010 at 5:23 Comment(2)
OK, I have got it working now! Never would have guessed it goes in the WEB-INF folder! I will use you idea of creating a JAR file for it later when I get a Ant script going.Hemihydrate
@Leith cool, but make that change (about jar files) sooner than later :)Kuchen

© 2022 - 2024 — McMap. All rights reserved.