简体   繁体   中英

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.

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").

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.

If you are using leiningen , after generating the jar with lein uberjar , do not use java -jar target/your-name.jar , but use java -jar target/your-name-standalone.jar .

Same thing for boot .

Are all dependencies available ? I'm not familiar with the clojure.lang.IFn class. Is it from an external library? If it is did you marked the dependency with the provided scope?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM