JBig2在运行时未解析?

huangapple go评论149阅读模式
英文:

JBig2 not resolved at runtime?

问题

我有一个使用PDFBox将PDF转换为PNG的Java/Gradle应用程序。在本地IDE上进行测试时,我的代码如下:

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);
}

在生产环境中,另一个应用程序会启动一个新的JVM来运行我的应用程序。我不确定父应用程序的类路径是什么,但如果在我的Gradle依赖中有以下内容,是否重要?

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

在本地IDE上测试我的主方法时,它可以正常工作,但在我描述的第二种设置中却无法正常工作。我还检查了我的清单文件,并可以看到以下文件:

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

我错过了什么?

英文:

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?

答案1

得分: 1

我将您的评论作为答案进行回复,因为否则格式/空间有限。

我们使用slf4j来“重定向”所有的commons logging到log4j。您需要将以下依赖项添加到日志中,然后在为pdfbox添加一个appender之后,它将记录到您的标准日志文件...

<!-- PdfBox正在使用Apache commons logging。要强制其使用log4j2,必须使用以下库 -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.7.30</version>
</dependency>

关于您的第二个问题:不一定。所有这些库是否都在类路径上可用?
如果没有任何错误消息,很难弄清楚到底发生了什么。您能看到控制台吗?如果可以的话,使用以下代码检查您的类是否存在。

try {
    Class<?> clazz = Class.forName("org.apache.pdfbox.pdmodel.PDDocument");
} 
catch (ClassNotFoundException e) {
    System.err.println("找不到PdfBox类!");
}
英文:

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...

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

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&lt;?&gt; clazz = Class.forName(&quot;org.apache.pdfbox.pdmodel.PDDocument&quot;);
} 
catch (ClassNotFoundException e) {
    System.err.println(&quot;PdfBox classes not found!&quot;);
}

huangapple
  • 本文由 发表于 2020年8月19日 00:34:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63472959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定