问题
Situation
I'm developing an application that uses Java's URLClassLoader and ServiceLoader to load jar files. Inside these jar files is the provider that implements my interface. The folder structure is as described in this post by oracle meaning that:
- The interface is in the same directory as the class that implements the interface in the plugin (com.x.projectname.plugin.IInterface.java). In the plugin, both the interface and the class that implements it are in the com.x.projectname.plugin folder.
- In the plugin there's a resources.META-INF.services directory with one file in it: com.x.projectname.plugin.IInterface with the following content: com.x.projectname.plugin.ClassThatImplementsIInterface.
When running on my local machine (tested with Oracle JDK 1.8 162 and OpenJDK 1.8 171) the plugin is loaded in fine and the application can use the plugin as desired.
The problem
When running the main application in a docker container it cannot load the desired plugin. The Docker container has a folder on the machine mounted inside it, where the plugin resides. The docker image of the application uses openjdk:8-jdk-alpine, but the problem persists whether I use the alpine version or not.
Upon trying to load the plugin using Serviceloader.load() (See ServiceLoader.load(InterfaceName.class, ClassLoader loader)
it crashes with the following error: Provider com.x.projectname.plugin.InterfaceImplementingClass not a subtype.
Here's the relevant part of the stacktrace:
Caused by: java.util.ServiceConfigurationError: com.x.projectname.plugin.IInterface: Provider com.x.projectname.plugin.ImplementingClass not a subtype
at java.util.ServiceLoader.fail(ServiceLoader.java:239) ~[na:1.8.0_151]
at java.util.ServiceLoader.access$300(ServiceLoader.java:185) ~[na:1.8.0_151]
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:376) ~[na:1.8.0_151]
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404) ~[na:1.8.0_151]
at java.util.ServiceLoader$1.next(ServiceLoader.java:480) ~[na:1.8.0_151]
at com.x.projectname.loader.PluginLoader.loadPlugins(PluginLoader.java:118) ~[classes!/:na]
at com.x.projectname.loader.PluginLoader.initializePluginLoading(PluginLoader.java:67) ~[classes!/:na]
at com.x.projectname.service.PluginService.<init>(PluginService.java:37) ~[classes!/:na]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_151]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_151]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_151]
Any ideas why it won't load plugins when ran inside a Docker container?
EDIT 1: Dockerfile and docker run command
FROM openjdk:8-jdk-alpine
ARG JAR_FILE
ADD ${JAR_FILE} /CICD-dashboard.jar
# Remote debugging port for intelliJ == address
EXPOSE 50505
ENTRYPOINT [ "java", "-Xrunjdwp:transport=dt_socket,address=50505,suspend=n,server=y", "-jar", "/X-project.jar"]
The docker run command:
docker run -d -v /home/folder/pluginfolder:/pluginfolder -p=50505:50505 image-name
回答1:
As it turns out, the answer was as Saqib said in a comment: build a fat jar. I used shadowJar for this: http://imperceptiblethoughts.com/shadow/#configuring_shadow.
来源:https://stackoverflow.com/questions/50040181/provider-not-a-subtype-inside-docker-container