Importing Ghost4J into CLJ

Hi ClojureVerse. I’m very fresh to FP, to Clojure, to Java, everything-- been through seom of Brave Clojure and 4Clojure so I have the basics down, but I’m stymied trying to put some of this to practical use.

Here’s my problem: clients submit photo-quality PDF scans, and we only need screen-quality, and the memory/bandwidth is getting expensive (and slow!). I need to reduce/compress these files.

I found that converting a pdf to ps and back with ghostscript reduces the file size by over 80-odd percent.

I also found a Java package Ghost4J that is a wrapper for Ghostscript. Yay! just import it and learn dot notation, right?

here’s the github repo: https://github.com/zippy1978/ghost4j

here’s the source page: http://www.ghost4j.org/

here’s the clojars page: https://clojars.org/net.sf/ghost4j

As far as I can tell, I’ve done everything to make sure my dependencies and imports are good. And my code compiles. However, when I try to run the ‘high-level api’ version of pdf to ps conversion as on the source page, I get

java.lang.NoClassDefFoundError: Could not initialize class org.ghost4j.GhostscriptLibrary

and a huge stacktrace. What are the steps I can take to ensure that I have, in fact, done everything I can? what is the big dumb obvious thing I’m missing? I went through the dependencies of the base class as well as GhostscriptLibrary, then walked through each import in core.clj to make sure the code compiles. Hit me with that facepalm, I’m ready for it.

Welcome to Clojure, @jollyblondgiant - you’re in for a real treat! :wink:

I’ve never used Ghost4J myself, so take what I say here with a large serving of salt… …but it looks as though Ghost4J loads native (C/C++) libraries in order to integrate with Ghostscript itself, and that can be a bit of a squirrelly experience, given how hard the JVM works to be platform independent.

My best guess, then, is that that loading process is going awry, and a quick google of your specific exception ("java.lang.NoClassDefFoundError: Could not initialize class org.ghost4j.GhostscriptLibrary") turned up this StackOverflow post.

Might be worth seeing if what’s described there is close to what you’re seeing?

What @pmonks said is most likely true. I also just wanted to say, depending where you intend to run this, if you have ghostcript available as a cli on your path, using it with Clojure sh function could be an option: https://clojuredocs.org/clojure.java.shell/sh