views:

39

answers:

1

Java's ServiceLoader needs those entries to be present inside the JAR file. Is there a way to programatically add those service entries at runtime time for unit testing, when inside the IDE? Especially when the JARs are not built yet.

A: 

Don't get too hung up focusing on JAR files. They are the preferred way to encapsulate services, but they aren't required. The key is really ClassLoader.getResources(String) - where the String arg effectively becomes ("META-INF/services/" + serviceClass.getName()). One other bit of information to keep in mind is that ServiceLoader.load(Class) makes use of the context class loader (of course, you can also make use of ServiceLoader.load(Class, ClassLoader)). So...what you really need to do is manipulate the classpath or configure the context class loader in such a way as to make ServiceLoader happy.

kschneid