Java Batik: ClassNotFoundException: org.apache.batik.dom.svg.SVGDOMImplementation

flixe picture flixe · Dec 9, 2015 · Viewed 6.9k times · Source

I have a servlet where i´m trying to create a pdf out of a string containing svg code. Therefore i have this code:

public class Conversion extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String svgString = req.getParameter("svg");
        StringReader svg = new StringReader(svgString);
        File pdf = new File("/tmp/chart.pdf");

        Transcoder transcoder = new PDFTranscoder();
        TranscoderInput transcoderInput = new TranscoderInput(svg);
        TranscoderOutput transcoderOutput = new TranscoderOutput(new FileOutputStream(pdf));

        try {
            transcoder.transcode(transcoderInput, transcoderOutput);
        } catch (TranscoderException e) {
            e.printStackTrace();
        }

        resp.setContentType("application/pdf");
        InputStream in = new FileInputStream(pdf);
        ServletOutputStream out = resp.getOutputStream();

        byte[] buffer = new byte[4096];
        while(in.read(buffer, 0, 4096) != -1){
           out.write(buffer, 0, 4096);
        }
        in.close();
        out.flush();
        out.close();
    }
}

When executing i get this error:

java.lang.ClassNotFoundException: org.apache.batik.dom.svg.SVGDOMImplementation
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1332)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166)
    at org.apache.fop.svg.AbstractFOPTranscoder.<init>(AbstractFOPTranscoder.java:107)
    at org.apache.fop.svg.PDFTranscoder.<init>(PDFTranscoder.java:84)
    at com.Webmap_read.Conversion.doGet(Conversion.java:38)
    at com.Webmap_read.Conversion.doPost(Conversion.java:68)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

When i check my Build Path, it seems like all the JARs that are contained in the Batik 1.8 archive file downloaded from: http://apache.lauf-forum.at/xmlgraphics/batik/binaries/. But the class SVGDOMImplementation is not there. How can i import all the necessary JARs/classes and why aren´t they contained in the standard zip? It´s driving me crazy right now... Or is there maybe an easier way to convert an SVG to PDF?

Answer

andrucz picture andrucz · Dec 9, 2015

Take a look at http://www.findjar.com/class/org/apache/batik/dom/svg/SVGDOMImplementation.html an check if any of listed jars are in application classpath.

I suggest you to download Batik from https://xmlgraphics.apache.org/batik/download.html (official website)