You forgot to require
the Clojure namespace:
Here is the project structure:
-rwxrwxr-x 1 alan alan 162 Sep 26 15:27 compile-run-java.bash*
-rwxrwxr-x 1 alan alan 439 Oct 19 2016 project.clj*
-rw-rw-r-- 1 alan alan 142 Sep 26 16:38 src/embedded_clojure/core.clj
-rw-rw-r-- 1 alan alan 586 Sep 26 15:21 src-java/mypkg/Main.java
-rw-rw-r-- 1 alan alan 125 Oct 19 2016 test/tst/embedded_clojure/core.clj
and the project.clj
file:
(defproject embedded-clojure "0.1.0-SNAPSHOT"
:dependencies [
[org.clojure/clojure "1.8.0"]
]
:java-source-paths ["src-java"]
:main embedded-clojure.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all}})
Here is the Java class that calls the embedded Clojure function:
package mypkg;
import clojure.java.api.Clojure;
import clojure.lang.IFn;
class Main {
public static void main( String[] args ) {
System.out.println( "Java Main.main()" );
// clojure.core is automatically "required"; you don't need to
IFn plus = Clojure.var("clojure.core", "+");
System.out.println( " plus: " + plus.invoke(1, 2) );
// any other namespace needs to be "required"
IFn require = Clojure.var("clojure.core", "require");
require.invoke(Clojure.read("embedded-clojure.core"));
IFn add = Clojure.var("embedded-clojure.core", "add");
System.out.println( " add: " + add.invoke(2, 3) );
}
}
Clojure file:
(ns embedded-clojure.core
(:gen-class))
(defn add [x y] (+ x y))
(defn -main [& args]
(println "Clojure -main: (add 4 5) =>" (add 4 5)))
Clojure test file:
(ns tst.embedded-clojure.core
(:use embedded-clojure.core
clojure.test))
(deftest simple-add
(is (= 13 (add 6 7))))
Run Script compile-run-java.bash
#!/bin/bash -v
lein clean
lein uberjar
# Use Java main()
java -cp /home/alan/demo/embedded-clojure/target/uberjar/embedded-clojure-0.1.0-SNAPSHOT-standalone.jar \
mypkg.Main
# Use Clojure -main
java -cp /home/alan/demo/embedded-clojure/target/uberjar/embedded-clojure-0.1.0-SNAPSHOT-standalone.jar \
embedded_clojure/core
and now we can run from the command line:
> ./compile-run-java.bash
#!/bin/bash -v
lein clean
lein uberjar
Compiling 1 source files to /home/alan/demo/embedded-clojure/target/uberjar/classes
Compiling embedded-clojure.core
Created /home/alan/demo/embedded-clojure/target/uberjar/embedded-clojure-0.1.0-SNAPSHOT.jar
Created /home/alan/demo/embedded-clojure/target/uberjar/embedded-clojure-0.1.0-SNAPSHOT-standalone.jar
# Use Java main()
java -cp /home/alan/demo/embedded-clojure/target/uberjar/embedded-clojure-0.1.0-SNAPSHOT-standalone.jar \
mypkg.Main
Java Main.main()
plus: 3
add: 5
# Use Clojure -main
java -cp /home/alan/demo/embedded-clojure/target/uberjar/embedded-clojure-0.1.0-SNAPSHOT-standalone.jar \
embedded_clojure/core
Clojure -main: (add 4 5) => 9
We can also use lein
to run the Clojure -main
or the Clojure tests:
> lein test
lein test tst.embedded-clojure.core
Ran 1 tests containing 1 assertions.
0 failures, 0 errors.
> lein run
Clojure -main: (add 4 5) => 9