【问题标题】:pdfbox application fat jar gives “Cannot read JBIG2 image: jbig2-imageio is not installed” but works ok running from IDEpdfbox 应用程序 fat jar 给出“无法读取 JBIG2 图像:未安装 jbig2-imageio”,但可以从 IDE 运行
【发布时间】:2020-05-04 13:49:26
【问题描述】:

我在构建使用 pdfbox 的应用程序时遇到问题。 当我从 IDE 运行该应用程序时(我使用 netbeans 8.1),该应用程序能够阅读带有 jbig2 图像的书籍(我在 pom.xml 中有 jbig2 的 maven 依赖项)。 问题是当我构建应用程序时创建了一个胖罐。 当我使用相同的输入 pdf 运行 fat jar 时,会出现以下错误:

“Cannot read JBIG2 image: jbig2-imageio is not installed”

评论该错误的线程似乎没有解决我的问题(他们说必须将 maven 依赖项添加到 pom,但该依赖项已经在我的 pom 上)。

我还检查了 jbig2 库类是否在 fat 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();
            }
        }
    }
}

我将整个独立的示例项目放在这里(目的是帮助解决问题): http://www.frojasg1.com/20200504.PdfImageExtractor.zip

当我从 IDE 运行该应用程序时,它会产生以下输出:

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 图像吗?

【问题讨论】:

  • Netbeans 内部和外部使用的 Java 完全相同吗?您可以打印出所有系统属性以查看。
  • 您的问题已在用户邮件列表中得到答复。
  • 是的,我在 pdfbox 用户邮件列表中发布了同样的问题。我会放一个指向那个答案的链接

标签: java maven pdfbox jbig2


【解决方案1】:

我在 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 jars 中合并它们

通过将这些行添加到 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>

问题解决了。

【讨论】:

  • 你好,我也有同样的问题,能否请你提供一份你的 fatjar 的副本,并在末尾提供正确的配置文件
  • 你好 mugiwaradz,我刚刚放假了。让我回家,我会在 github 上准备一个项目,供任何需要的人使用。
  • 你好 mugiwaradz,我刚刚在我的 github 上创建了一个新的存储库。有一个库可以使用那些在将它们一起添加到胖 jar 中时覆盖它们的 metainf 文件的库。在那个新库中,有一个文件夹,其中包含新合并的 metainf 文件。看看:github.com/frojasg1/libGen/blob/master/gpl/own/libPdfboxWrapper/… 和上:github.com/frojasg1/libGen/tree/master/gpl/own/libPdfboxWrapper/…
【解决方案2】:

添加 ImageIO.scanForPlugins(); ImageIO.scanForPlugins() 之前; 也可以解决这个问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多