简体   繁体   English

从jar中打包的库访问类

[英]Accessing classes from libraries packaged in a jar

I have developed a plugin for an application. 我已经为应用程序开发了一个插件。 This plugin includes several libraries 这个插件包括几个库

When the plugin is called from the application I get a: 当从应用程序调用插件时,我得到:

java.lang.NoClassDefFoundError: com/sun/media/imageioimpl/plugins/tiff/TIFFImageWriterSpi

I have confirmed that this class has been packaged with the plugin jar so I am guessing that I need to use some sort of classloader in the plugin jar to access this class - but not sure how to. 我已经确认该类已与插件jar打包在一起,所以我猜测我需要在插件jar中使用某种类加载器来访问此类-但不确定如何进行。 Any help or re-direction appreciated. 任何帮助或重定向赞赏。

You need to let the application know where the plugin jars are located so that it can load the required classes at run time. 您需要让应用程序知道插件jar的位置,以便它可以在运行时加载所需的类。 You can do that by specifying the Class-Path entry ,that refers to the plugin jars, in the META-INF/MANIFEST file of your application jar. 您可以通过在应用程序jar的META-INF / MANIFEST文件中指定指向插件jar的Class-Path条目来实现。

Class-Path: plugin-jar-name1 plugin-jar-name2

you need to add com.sun.media:jai_imageio:1.1 dependency into your pom.xml . 您需要将com.sun.media:jai_imageio:1.1依赖项添加到pom.xml you can find dependency belwo: 您可以找到依赖项belwo:

<dependency>
    <groupId>com.sun.media</groupId>
    <artifactId>jai_imageio</artifactId>
    <version>1.1</version>
</dependency>

if your are using simple project you need to download jai_imageio.jar jar file from below link and put into lib directory and add into build path: 如果您使用的是简单项目,则需要从以下链接下载jai_imageio.jar jar文件,并将其放入lib目录并添加至构建路径:

http://www.java2s.com/Code/Jar/j/Downloadjaiimageiojar.htm http://www.java2s.com/Code/Jar/j/Downloadjaiimageiojar.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM