1

Is it possible to build a JAR file from a clojure file using the jar cf jar-file input-file(s) command, and then running it on another machine (without clojure) like with java file command?

What is the best way to execute a clojure program on another computer which has access to java but not to clojure?

2 Answers 2

1

Absolutely!

The easiest way to do this is to use Leiningen to manage your project. Once you have a project.clj set up, it's as simple as calling:

lein uberjar

to generate a portable .jar file.

Sign up to request clarification or add additional context in comments.

7 Comments

so the steps would be: lein new my-file cd to the directory, lein uberjar and then i could send the project.clj file for example per email to some one, he runs java project.clj? Am i right?
No, project.clj manages things like version, dependencies, etc. Lein requires it, and lein uberjar generates a jar file that you could send to someone and run with java.
well i am getting an error, i cant see what am i doing false: i created the new project called test (lein new test), then ran lein uberjar from inside the test directory, which creates two jar files inside the target dir : test-0.1.0-SNAPSHOT.jar and test-0.1.0-SNAPSHOT-standalone.jar. then i run from inside the target directory java test-0.1.0-SNAPSHOT.jar and it says: Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar Error: Could not find or load main class test-0.1.0-SNAPSHOT.jar
In your project.clj, you need to specify a key/value pair :main somenamespace. It will look in that namespace for a -main function.
what should this namespace then be? The same ns as in the src/file-name/core.clj ?
|
1

In your core.clj you need to add gen-class in your ns macro like this:

(ns test.core
   ...........
   (:gen-class))

next run lein uberjar next run from your project root folder

java -jar target/test...standalone.jar

7 Comments

i did this, it starts up the REPL! Is the project root folder the test folder created as i runned lein new test? This is how you run clojure programs as java executable? Does it have to do with maybe some version differences of lein or something like that? By the way this is my test/src/test/core.clj: (ns test.core (:gen-class)) (defn foo "I don't do a whole lot." [x] (println x "Hello, World!"))
It worked for me. I just uploaded a demo project here github.com/bit-twit/konstantinopol . Can you check that out ? Just check core.clj and project.clj see if it makes any difference
Yup, the folder created by lein new test. You should see the project.clj file in the root folder.
If I do lein version I get Leiningen 2.5.1 on Java 1.8.0_45 Java HotSpot(TM) 64-Bit Server VM , what do you get ?
i get: Leiningen 2.5.1 on Java 1.7.0_79 OpenJDK Server VM
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.