This project contains examples related to using gen-class to generate
Java-style classes from within Clojure.
The
com.clojurebook.imaging
namespace defines a Clojure API for loading and resizing images. It then uses gen-class to produce a Java class (ResizeImage in the default package) whose static utility methods delegate to the API's resize-image function.
You can see this in action by first building an uberjar containing all of the code in the project, and its dependencies:
$ lein uberjar
Then you can choose to run either ResizeImage directly:
$ java -cp target/gen-class-1.0.0-standalone.jar ResizeImage clojure.png small.png 0.3
…or you can run a Java shim class that calls
into ResizeImage to demonstrate that Java->Clojure interop:
$ java -cp target/gen-class-1.0.0-standalone.jar ResizeClient clojure.png java-small.png 0.3
The
com.clojurebook.CustomException
namespace defines a custom Exception subclass that has a couple of interesting properties:
- a
java.util.Mapvalue can be provided when constructing an instance ofCustomException CustomExceptionimplements Clojure'sIDerefinterface, so instances can be dereferenced (i.e.@e) to easily obtain the aforementionedjava.util.Map.
The BatchJob class shows an example of using this
custom Exception type from Java. Assuming you've built an uberjar
(lein uberjar), you can run BatchJob's main method, which echos
the contents of the exception's info map:
$ java -cp target/gen-class-1.0.0-standalone.jar BatchJobError! Operation failed {"timestamp" 1333488510315, "customer-id" 89045, "priority" "critical", "jobId" "verify-billings"}