FOP蜡染NoClassDefFoundError:org / apache / batik / bridge / UserAgent

时间:2017-11-15 14:17:05

标签: xsl-fo apache-fop batik

我不使用maven,pom或任何东西。我只是有简单的测试类测试,我只是尝试使XSL-FO转换为PDF,我得到了这个错误。 代码很简单:

package testing;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; 

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.sax.SAXResult; 

import javax.xml.transform.Result; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.stream.StreamSource; 

import org.apache.fop.apps.Fop; 
import org.apache.fop.apps.FopFactory; 

import org.apache.xmlgraphics.util.MimeConstants;


    public class test {

    public static void main(String[] args) {
        String pth="C:/Temp/";
        FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
        TransformerFactory factory = TransformerFactory.newInstance();
        OutputStream out;
        try
        {   
        out = new BufferedOutputStream(new FileOutputStream(new File(pth+"myfile.pdf")));

        InputStream in = new FileInputStream(pth+"everything.fo");

        Source source = null;
        Transformer transformer = factory.newTransformer();
        source = new StreamSource(in);      
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
        Result res = new SAXResult(fop.getDefaultHandler());
        transformer.transform(source, res);         
        } catch (Exception e)
        {
            e.printStackTrace();;
        }       
    }
}

这就是结果:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/batik/bridge/UserAgent
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    at java.lang.Class.getConstructor0(Class.java:3075)
    at java.lang.Class.getDeclaredConstructor(Class.java:2178)
    at org.apache.xmlgraphics.util.Service.providers(Service.java:85)
    at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.discoverClasspathImplementations(ImageImplRegistry.java:115)
    at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.<init>(ImageImplRegistry.java:79)
    at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.<init>(ImageImplRegistry.java:87)
    at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.<clinit>(ImageImplRegistry.java:71)
    at org.apache.xmlgraphics.image.loader.ImageManager.<init>(ImageManager.java:64)
    at org.apache.fop.apps.FopFactoryBuilder$FopFactoryConfigImpl.<init>(FopFactoryBuilder.java:381)
    at org.apache.fop.apps.FopFactoryBuilder.<init>(FopFactoryBuilder.java:90)
    at org.apache.fop.apps.FopFactoryBuilder.<init>(FopFactoryBuilder.java:81)
    at org.apache.fop.apps.FopFactoryBuilder.<init>(FopFactoryBuilder.java:71)
    at org.apache.fop.apps.FopFactory.newInstance(FopFactory.java:143)
    at testing.test5.main(test5.java:37)
Caused by: java.lang.ClassNotFoundException: org.apache.batik.bridge.UserAgent
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 16 more

这是我的libs: My Libs

我做错了什么? 正如我所说,我不使用POM,MAVEN等,只是用于测试目的的简单java代码。我知道有一些依赖,但没有POM我怎么能管理这些东西?

这是我的FO文件: everything.fo

1 个答案:

答案 0 :(得分:0)

关于这种麻烦,我已经使用最新版本的蜡染解决了我的问题。

在POM.xml中,我有这个:

<dependency> 
<groupId>org.apache.xmlgraphics</groupId> 
<artifactId>batik-bridge</artifactId> 
<version>1.11</version> 
</dependency>