您好我正在尝试使用FOP将xml文件转换为pdf并且我一直遇到错误,现在我有错误
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlgraphics/io/ResourceResolver
at org.apache.fop.apps.FopConfParser.<init>(FopConfParser.java:122)
at org.apache.fop.apps.FopFactory.newInstance(FopFactory.java:132)
at run.ExampleFO2PDF.main(ExampleFO2PDF.java:62)
我在构建路径中有 FOP 库以及 xmlgraphics 和 commons logging 和 Avalon framework
这是我正在尝试运行的代码
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.*;
import org.xml.sax.SAXException;
public class pdf{
public static void main(String[] args) throws SAXException, IOException, TransformerException
{
FopFactory fopFactory = FopFactory.newInstance(new File("."));
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("D:/temp/myfile.pdf")));
try {
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(); // identity transformer
Source src = new StreamSource(new File("D:/temp/test.xml"));
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
} finally {
//Clean-up
out.close();
}
}
}
答案 0 :(得分:5)
abstract interface org.apache.fop.apps.MimeConstants implements org.apache.xmlgraphics.util.MimeConstants
org.apache.xmlgraphics.util.MimeConstants
属于 xmlgraphics-commons-1.5.jar 。只需在您的类路径中包含 xmlgraphics-commons-1.5.jar ,它就可以解决问题。
答案 1 :(得分:2)
我猜你已经想出了这一个 - 但无论如何还没有: FOP 1.1的主干确实有引用xmlgraphics.io.ResourceResolver的FopConfParser
和xmlgraphics commons的主干确实有ResourceResolver:
所以作为解决方案,更新到trunk!快乐的编码:)
答案 2 :(得分:2)
真的很老的线程,但是我花了这么长时间来解决这个问题,所以对于从FOP 1.1到2.0的迁移,这样的改变就像这样:
从:
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>1.1</version>
</dependency>
于:
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>2.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>xmlgraphics-commons</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-svg-dom</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-bridge</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-awt-util</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-gvt</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-transcoder</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-extension</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-ext</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>xmlgraphics-commons</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-svg-dom</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-bridge</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-awt-util</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-gvt</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-transcoder</artifactId>
<version>1.8</version>
<exclusions>
<exclusion>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-extension</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-ext</artifactId>
<version>1.8</version>
</dependency>
&#13;
答案 3 :(得分:1)
旧线程,为完整性添加更多内容。
当从0.92b更新到FOP 1.1并且未找到MimeConstants类时,我遇到了类似的问题。
我通过在我使用的FOP版本的源代码发行版中使用xmlgraphics-commons版本来解决它。
尝试使用构建FOP版本时使用的xmlgraphics-commons jar,可以在FOP源代码分发的lib文件夹中找到。
答案 4 :(得分:0)
我找不到xmlgraphics
jar,但我找到了xmlgraphics-commons。那是你的意思吗?
在我上面链接的页面中,似乎它不包含org.apache.xmlgraphics.io
包,但它确实包含org.apache.xmlgraphics.util.io
。也许你的意思是那个?