且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

即使类与META-INF / services位于同一JAR文件中,服务加载程序也找不到服务提供程序类

更新时间:2022-06-05 17:50:58

从此文档中
http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/invocation.html

当一个线程附加到VM时,上下文类加载器就是引导加载程序。

"When a thread is attached to the VM, the context class loader is the bootstrap loader."

通过AttachCurrentThread连接到JVM的任何本机线程()只获取引导类加载器,甚至不获取系统类加载器。除非您明确修复新线程的上下文类加载器,否则ServiceLoader引用的类将不可用。

Any native thread attached to the JVM via AttachCurrentThread() gets only the bootstrap class loader, not even the system class loader. Classes referenced by ServiceLoader will not be available unless you explicitly fix up the new thread's context class loader.

这可以这样做:

java.lang.Thread.currentThread().setContextClassLoader(
    java.lang.ClassLoader.getSystemClassLoader()
);