即使类与META-INF / services位于同一JAR文件中,服务加载程序也找不到服务提供程序类 [英] Service loader does not locate service provider class, even though class is in same JAR file as META-INF/services
问题描述
我正在成功运行一个C ++应用程序,该应用程序使用JAR文件作为类路径参数加载JVM。然后,应用程序成功使用JNI调用来执行此JAR文件中.class文件中定义的各种函数。
I am successfully running a C++ application that loads a JVM with a JAR file as a classpath argument. The application then successfully uses JNI calls to execute various functions defined in .class files within this JAR file.
包含在.jar文件的目录结构中的是第三方集.class文件 - 从jai_imageio.jar合并的文件(这些.class文件及其完整目录结构,使用Intellij IDEA合并到此单个.jar文件中)。合并的.jar文件中还包含原始jai_imageio.jar的 manifest.mf
中的行 - 特别是 implementation-title
和相关的行。此外,还存在 meta-inf / services
文件夹,也是从jai_imageio.jar复制的。 services
目录中列出的各种服务看起来是正确的。
Included in the .jar file's directory structure is a 3rd-party set of .class files - those merged from jai_imageio.jar (these .class files, with their full directory structure, were merged into this single .jar file using Intellij IDEA). Also included in the merged .jar file are the lines from the original jai_imageio.jar's manifest.mf
- in particular implementation-title
and related lines. Also, the meta-inf/services
folder is present, also copied from jai_imageio.jar. The various services listed within the services
directory look correct.
特别是 javax。 .jar文件中
包含单行 meta-inf / services
文件夹中的imageio.spi.ImageOutputStreamSpi com。 sun.media.imageioimpl.stream.ChannelImageOutputStreamSpi
,并且.jar文件中有一个与此对应的类正好在该行指示的目录中: com / sun / media /imageioimpl/stream/ChannelImageOutputStreamSpi.class
。
In particular, javax.imageio.spi.ImageOutputStreamSpi
within the meta-inf/services
folder in the .jar file contains the single line com.sun.media.imageioimpl.stream.ChannelImageOutputStreamSpi
, and there is a class corresponding to this within the .jar file at exactly the directory indicatted by that line: com/sun/media/imageioimpl/stream/ChannelImageOutputStreamSpi.class
.
但是,当Java代码执行以下行时:
However, when the Java code executes the following line:
ImageIO.write(image, "tiff", file); // Assume 'image' is a BufferedImage and 'file' is a File
...它会抛出例外:
... it throws an exception:
java.util.ServiceConfigurationError: javax.imageio.spi.ImageOutputStreamSpi:
Provider com.sun.media.imageioimpl.stream.ChannelImageOutputStreamSpi not found
...即使此类是出现在同一个.jar文件中,如上所述。
... even though this class is present within the same .jar file, as noted above.
有人可以解释为什么会发生这种错误,以及我应该怎么做才能解决它。
Can somebody please explain why this error is happening, and what I should do to resolve it.
推荐答案
从此文档中
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()
);
这篇关于即使类与META-INF / services位于同一JAR文件中,服务加载程序也找不到服务提供程序类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!