Java pdfbox应用程序fat jar给出“;无法读取JBIG2映像:未安装JBIG2映像IO”;但是在IDE上运行也可以
我在构建使用pdfbox的应用程序时遇到了一个问题。 当我从IDE运行jbig2图像时,应用程序能够阅读带有jbig2图像的书籍(我使用netbeans 8.1)(我在pom.xml中有jbig2的maven依赖项)。 问题是当我构建应用程序时,创建了一个胖罐子。 当我使用相同的输入pdf运行fat jar时,会出现以下错误:Java pdfbox应用程序fat jar给出“;无法读取JBIG2映像:未安装JBIG2映像IO”;但是在IDE上运行也可以,java,maven,pdfbox,jbig2,Java,Maven,Pdfbox,Jbig2,我在构建使用pdfbox的应用程序时遇到了一个问题。 当我从IDE运行jbig2图像时,应用程序能够阅读带有jbig2图像的书籍(我使用netbeans 8.1)(我在pom.xml中有jbig2的maven依赖项)。 问题是当我构建应用程序时,创建了一个胖罐子。 当我使用相同的输入pdf运行fat jar时,会出现以下错误: “Cannot read JBIG2 image: jbig2-imageio is not installed” 评论该错误的线程似乎并没有解决我的问题(他们说必须向
“Cannot read JBIG2 image: jbig2-imageio is not installed”
评论该错误的线程似乎并没有解决我的问题(他们说必须向pom添加maven依赖项,但该依赖项已经在我的pom上)
我还检查了jbig2库类是否在胖jar中,所以我不知道发生了什么
我在一个小应用程序中隔离了这个问题,如下所示:
public static void main( String[] args )
{
String fileName = null;
if( args.length == 0 )
{
fileName = "test.pdf";
}
else
{
fileName = args[0];
}
PdfDocumentWrapper doc = null;
try
{
PdfboxFactory factory = new PdfboxFactory();
doc = factory.createPdfDocumentWrapper();
doc.loadPdf( fileName );
for( int ii = 0; ii < doc.getNumberOfPages(); ii++ )
{
int pageNum = ii+1;
System.out.println("\n\nProcessing page: " + pageNum +"\n---------------------------------");
List<ImageWrapper> imageList = doc.getImagesOfPage(ii);
int jj=0;
for( ImageWrapper image: imageList )
{
jj++;
System.out.println(String.format(" Page[%d]. Image[%d] -> bounds: %s",
pageNum, jj, image.getBounds().toString() ) );
}
}
}
catch( Exception ex )
{
ex.printStackTrace();
}
finally
{
if( doc != null )
{
try
{
doc.close();
}
catch( Exception ex )
{
ex.printStackTrace();
}
}
}
}
当我从命令行运行应用程序时,它会给出以下输出:
Processing page: 1
---------------------------------
Page[1]. Image[1] -> bounds: java.awt.Rectangle[x=17,y=33,width=442,height=116]
Page[1]. Image[2] -> bounds: java.awt.Rectangle[x=53,y=513,width=376,height=124]
Page[1]. Image[3] -> bounds: java.awt.Rectangle[x=101,y=250,width=285,height=5]
------------------------------------------------------------------------
$ java -jar ./PdfImageExtractor-v1.0-SNAPSHOT-all.jar
Processing page: 1
---------------------------------
may 04, 2020 3:40:18 PM org.apache.pdfbox.contentstream.PDFStreamEngine operatorException
GRAVE: Cannot read JBIG2 image: jbig2-imageio is not installed
may 04, 2020 3:40:18 PM org.apache.pdfbox.contentstream.PDFStreamEngine operatorException
GRAVE: Cannot read JBIG2 image: jbig2-imageio is not installed
may 04, 2020 3:40:18 PM org.apache.pdfbox.contentstream.PDFStreamEngine operatorException
GRAVE: Cannot read JBIG2 image: jbig2-imageio is not installed
有人知道为什么胖罐子不能读取jbig2图像吗?我在pdfbox用户邮件列表中发布了相同的问题,下面是答案:
Your fat-jar consists of several ImageIO libs. You are simply merging all files
to one big jar and overwriting the config files of those ImageIO libs. Have a
look at the directory "/META-INF/services". The files of the JBig" plugin are
overwritten by files of another plugin. Either you merge those files or don't
create one big jar of all deps.
解决方案是:
非常感谢,就是这样
我已经能够创建这些META-INF文件:
$ find src/serviceManifests/
src/serviceManifests/
src/serviceManifests/META-INF
src/serviceManifests/META-INF/services
src/serviceManifests/META-INF/services/javax.imageio.spi.ImageReaderSpi
src/serviceManifests/META-INF/services/javax.imageio.spi.ImageWriterSpi
将它们与ImageIO JAR中的合并
通过将以下行添加到pom.xml:
<properties>
<service.declaration.dir>src/serviceManifests</service.declaration.dir>
<service.files.path>META-INF/services</service.files.path>
</properties>
<build>
<resources>
...
<resource>
<directory>${service.declaration.dir}</directory>
<includes>
<include>${service.files.path}/*</include>
</includes>
</resource>
...
</resources>
...
</build>
src/serviceManifests
META-INF/服务
...
${service.declaration.dir}
${service.files.path}/*
...
...
问题已解决。添加ImageIO.scanForPlugins();在ImageIO.scanForPlugins()之前;
也可以解决这个问题与Netbeans内外使用的Java完全相同吗?您可以打印出所有要查看的系统属性。您的问题已在用户邮件列表中得到回答。是的,我已在pdfbox用户邮件列表中发布了相同的问题。我会放一个链接到这个答案你好,我有同样的问题,你能给我一份你的fatjar的副本和正确的配置文件在endsHello mugiwaradz,刚才我在度假。让我回家,我将在github中为任何需要的人准备一个项目。有一个库可以利用这些库,当它们一起添加到胖jar中时,这些库会覆盖它们的metainf文件。在新库中,有一个文件夹包含新的合并metainf文件。看看:和: