Search code examples
javagradlepdfboxjbig2

JBig2 not resolved at runtime?


I have an Java/Gradle application that uses PDFBox to convert PDFs to PNGs. While testing locally on my IDE, my code is as follows:

public static void main(String[] args) throws IOException {
    PDDocument doc = PDDocument.load(new File("pdfFile.pdf"));
    PDFRenderer renderer = new PDFRenderer(doc);
    OutputStream os = new FileOutputStream(new File("image.png"));
    ImageIO.write(renderer.renderImageWithDPI(0, 300), "png", os);
}

In prod, another application launches a new JVM that runs my application. I'm not sure what the classpath for the parent application is, but if I have the following in my Gradle dependencies, does it matter?

implementation "org.apache.pdfbox:jbig2-imageio:3.0.2"

While testing my main method locally on my IDE, it works fine but not using the second setup I described. I have also checked my manifest and can see the following files:

META-INF/services/javax.imageio.spi.ImageReaderSpi
META-INF/services/javax.imageio.spi.ImageWriterSpi 

What am I missing?


Solution

  • I reply to your comment as an answer since otherwise formatting/space is limited.

    We use slf4j to "redirect" all commons logging to log4j. You have to add the following dependency to your log - then after also adding an appender for pdfbox it will log to your standard logfile...

    <!-- PdfBox is using Apache commons logging. To force this to use log4j2 the following lib has to be used -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.7.30</version>
    </dependency>
    

    To your 2nd question: No not necessarily. Are all that libs are available on the classpath? Without any error message it is really hard to figure out what exactly is going on. Are you able to see the console? If so then use the following code to check whether your classes are there.

    try {
        Class<?> clazz = Class.forName("org.apache.pdfbox.pdmodel.PDDocument");
    } 
    catch (ClassNotFoundException e) {
        System.err.println("PdfBox classes not found!");
    }