【问题标题】:Getting NoClassDefFoundError by using Ghost4j使用 Ghost4j 获取 NoClassDefFoundError
【发布时间】:2014-09-29 15:59:19
【问题描述】:

这是我在这里的第一次探索,我没有找到任何解决问题的方法。 如果我的文字是英文的,请不要误会。

对于我的程序,我想调整现有 PDF 文档中的图像大小。这应该在 Java 程序中自动发生。 在搜索过程中,我在网上找到了 Ghost4j 库,它可以解决我的问题——也许吧!

作为第一次使用 Ghost4j 测试它是否有效,我想从 MySQL 数据库中加载我的 PDF 文档并检查 pageCount。

这是我的短代码:

... 
for (File file : convertableFiles) {
        InputStream inputStream = new ByteArrayInputStream(file.getFile());

        PDFDocument doc = new PDFDocument();
        doc.load(inputStream);
        System.out.println(doc.getPageCount());
}
...

错误出现在第 45 行 = doc.load(inputStream)

(注意:如果我对 doc.load 使用 new File(Path) 并设置一个 pdfSample 文档。它可以工作。但不能使用 inputStream)

当我执行我的程序时,我每次都会得到这个 Excption:

Sep 29, 2014 4:54:53 PM ch.carauktion.dbresize.DBFileResizer convert
INFORMATION: P1 (asc): 0 / 1
Sep 29, 2014 4:54:54 PM ch.carauktion.dbresize.DBFileResizer run
SCHWERWIEGEND: P1 (asc): Exception
java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1OctetString
    at com.lowagie.text.pdf.PdfEncryption.<init>(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.readDecryptedDocObj(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.readDocObj(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.readPdf(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.<init>(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.<init>(Unknown Source)
    at org.ghost4j.document.PDFDocument.load(PDFDocument.java:45)
    at ch.carauktion.dbresize.pdf.DBPdfResizer.convertFiles(DBPdfResizer.java:50)
    at ch.carauktion.dbresize.DBFileResizer.convert(DBFileResizer.java:114)
    at ch.carauktion.dbresize.DBFileResizer.run(DBFileResizer.java:59)
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.asn1.ASN1OctetString
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 10 more

对于这个项目实现的Libraries,全部来自下载的ghost4j包:

  • ghost4j-0.5.1
  • iText-2.1.7
  • jna-3.3.0
  • log4j-1.2.15
  • commons-logging-1.1.1
  • commons-io-1.3.1
  • commons-beanutils-1.8.3

我搜索此错误时的示例站点:

http://sourceforge.net/p/itext/mailman/itext-questions/thread/4F422974.1070002@redlab.be/

http://itext-general.2136553.n4.nabble.com/java-lang-NoClassDefFoundError-org-bouncycastle-asn1-ASN1OctetString-td3427288.html

我知道 iText 2.1.7 不再受支持,我应该使用 5.x.x 版本,但在这里下载最新的 iText Lib 不起作用,而在 Ghost4j Jar 中显然 Lib 2.1.7 是用过的。 否则,可能是我的错,我现在不明白如何正确实现最新版本。

PS: 我正在使用 Java 1.7、Eclipse Kepler、Windows 8.1

我会很高兴,有人知道任何解决方案或可以帮助我一点。

武德曼

【问题讨论】:

    标签: java itext bouncycastle pdf-conversion ghost4j


    【解决方案1】:

    您缺少 Bouncycastle 依赖项。

    我认为 PDF 库不会依赖于此,除非需要保护 PDF,但您会在这里找到 Bouncycastle:http://bouncycastle.org/latest_releases.html

    尝试使用bcprov-jdk14-147.jar 和/或bcprov-ext-jdk14-147.jar 可从Maven Central repository 下载:

    如果还是不行,试试这里列出的the other excluded dependencies

    <dependency>
        <groupId>com.lowagie</groupId>
        <artifactId>itext</artifactId>
        <version>2.1.7</version>
        <exclusions>
            <exclusion>
                <artifactId>bcmail-jdk14</artifactId>
                <groupId>bouncycastle</groupId>
            </exclusion>
            <exclusion>
                <artifactId>bcmail-jdk14</artifactId>
                <groupId>org.bouncycastle</groupId>
            </exclusion>
            <exclusion>
                <artifactId>bcprov-jdk14</artifactId>
                <groupId>bouncycastle</groupId>
            </exclusion>
            <exclusion>
                <artifactId>bcprov-jdk14</artifactId>
                <groupId>org.bouncycastle</groupId>
            </exclusion>
            <exclusion>
                <artifactId>bctsp-jdk14</artifactId>
                <groupId>org.bouncycastle</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    

    注意:您应该使用 Maven 来获取这些依赖项。

    【讨论】:

    • 尝试使用 bcprov-jdk15on-151.jar 和 bcprov-ext-jdk15on-151.jar - 我不认为使用 bc 1.51 是一个好主意。 IText 2.1.7 很古老,很可能在 1.47 破裂之前依赖于 bc API。
    • 我考虑到了这一点(以及 pom.xml 依赖项)。
    • 非常感谢您的回答。它工作正常。我认为这是 iText 的内部错误 :)。
    • 虽然可以,但我建议您尝试使用 maven/gradle/etc。我之所以回答只是因为我知道如何在全球范围内解决NoClassDefFoundErrorClassNotFoundException,而不是因为我知道ghost4j 或iText。而且由于 Ghost4j 排除了这些依赖项,因此可能有一些原因(我不知道!)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 2011-11-05
    • 2020-03-16
    • 2012-07-29
    相关资源
    最近更新 更多