Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a maven-packaged clojure application from the jar

I have the following contents in src/main/clojure/za/co/pb/maven_test/test.clj file:

(ns za.co.pb.maven-test.test
  (:gen-class))

(defn -main []
  (println "Hello world!"))

I also have a POM that has the necesary dependencies on clojure-maven-plugin with the compile execution.

If I execute a mvn package command, I get a target/maven-test-1.0-SNAPSHOT.jar file and if I look in the classes folder I have these files in the folder target/classes/za/co/pb/maven_test:

  • test.class
  • test.clj
  • test__init.class
  • test$loading__4410__auto__.class
  • test$_main.class

This, as far as I know, is appropriate.

However, when I run this command:

java -cp target\app-1.0-SNAPSHOT.jar za.co.pb.maven_test.test

I get this:

Exception in thread "main" java.lang.NoClassDefFoundError: clojure/lang/IFn
Caused by: java.lang.ClassNotFoundException: clojure.lang.IFn
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: za.co.pb.maven_test.test. Program will exit.
like image 955
Pieter Breed Avatar asked Dec 28 '22 06:12

Pieter Breed


2 Answers

You don't have the clojure jars in the classpath. You can either embed them as per Stuart's response, or if you don't like having all the dependencies embedded in a single jar, you can use the dependency plugin and the jar plugin to get this working nicely.

See: http://groups.google.com/group/enclojure/msg/87159854fcb0e708 for a summary (note, there's a typo in the linked post, the package should be called "foo").

like image 95
Steve Lindsay Avatar answered Feb 08 '23 23:02

Steve Lindsay


You need to generate a JAR file file that includes all the dependencies of your project. The Maven Assembly Plugin does this for you, using the built-in jar-with-dependencies descriptor.

like image 32
Stuart Sierra Avatar answered Feb 09 '23 00:02

Stuart Sierra