在tomcat上使用Ghost4j和MaxProcessCount会导致jna错误

时间:2013-08-26 10:00:10

标签: java multithreading tomcat jna ghost4j

我正在尝试开发一种将pdf文档转换为jpeg图像的网络服务(基于平针织)。 我之所以选择GhostScript是因为我对它有很好的体验并且结果很好(特别是使用embedet字体)。所以我搜索了一种使用GhostScript和Java的方法,并找到了Ghost4j。

所以我将所有Ghost4j jar放入我的应用程序li​​b文件夹(也是jna.jar)。在我的第一次测试中,我遇到了多次执行任务的问题,因为jna在多次启动时会抛出错误。 所以我将jna.jar放入tomcat lib文件夹。这样做有点好,但它一次只执行一项任务。如果我同时开始另一个,没有任何事情发生。我似乎没有执行其他任务。

所以我尝试setMaxProcessCount(2);允许我的应用程序一次执行多个任务。这是我的代码:

private static void generateImages(String inputFile, String outputPath) throws IOException, RendererException, org.ghost4j.document.DocumentException {
        PDFDocument document = new PDFDocument();
        document.load(new File(inputFile));
        SimpleRenderer renderer = new SimpleRenderer();
        renderer.setMaxProcessCount(2);
        renderer.setResolution(150);
        renderer.setAntialiasing(4);
        System.setProperty("jna.library.path", "C:\\Program Files\\gs\\gs9.09\\bin\\");
        List<Image> images = renderer.render(document);
        for (int i = 0; i < images.size(); i++) {
                ImageIO.write((RenderedImage) images.get(i), "jpeg", new File(outputPath + File.separatorChar + (i + 1) + ".jpeg"));
        }
}

但如果我正在尝试执行我的任务,Ghost4j会抛出此错误:

org.ghost4j.renderer.RendererException: java.lang.Exception: java.lang.NoClassDefFoundError: com/sun/jna/Structure
    at org.ghost4j.renderer.AbstractRemoteRenderer.render(AbstractRemoteRenderer.java:133)
    at PdfResource.ConversionTask.generateImages(ConversionTask.java:218)
    at PdfResource.ConversionTask.exec(ConversionTask.java:58)
    at PdfResource.Task.run(Task.java:86)
    at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.Exception: java.lang.NoClassDefFoundError: com/sun/jna/Structure
    at gnu.cajo.invoke.Remote.invoke(Remote.java:594)
    at gnu.cajo.invoke.Remote.invoke(Remote.java:722)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
    at sun.rmi.transport.Transport$1.run(Transport.java:177)
    at sun.rmi.transport.Transport$1.run(Transport.java:174)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273)
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:160)
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
    at com.sun.proxy.$Proxy165.invoke(Unknown Source)
    at gnu.cajo.invoke.Remote.invoke(Remote.java:565)
    at org.ghost4j.renderer.AbstractRemoteRenderer.render(AbstractRemoteRenderer.java:126)
    ... 4 more
Caused by: java.lang.NoClassDefFoundError: com/sun/jna/Structure
    at org.ghost4j.renderer.SimpleRenderer.run(SimpleRenderer.java:68)
    at org.ghost4j.renderer.AbstractRemoteRenderer.remoteRender(AbstractRemoteRenderer.java:64)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at gnu.cajo.invoke.Remote.invoke(Remote.java:582)
    at gnu.cajo.invoke.Remote.invoke(Remote.java:722)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
    at sun.rmi.transport.Transport$1.run(Transport.java:177)
    at sun.rmi.transport.Transport$1.run(Transport.java:174)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    ... 1 more
Caused by: java.lang.ClassNotFoundException: com.sun.jna.Structure
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 23 more

我想它与Ghost4j正在启动的额外JVM有一些东西,它似乎没有使用tomcat使用的相同类路径,但我不是真正的java专家,也不知道如何解决这个问题问题

0 个答案:

没有答案