├── .gitignore ├── README.md ├── build.clj ├── clj └── src │ └── my │ └── main.clj ├── deps.edn └── java └── src └── my └── project └── Thing.java /.gitignore: -------------------------------------------------------------------------------- 1 | .calva/output-window 2 | .clj-kondo/.cache 3 | .cpcache 4 | .lsp/.cache 5 | .portal/vs-code.edn 6 | .nrepl-port 7 | target/ 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Combining Java and Clojure in a deps.edn project 2 | 3 | > Updated 5/26/2023 to use `build.clj`. 4 | 5 | The Clojure source code is in `clj/src`. 6 | The Java source code is in `java/src`. 7 | 8 | 9 | `deps.edn` puts both `clj/src` and `target/classes` on the classpath, 10 | and it has a `:build` alias for `tools.build` usage. 11 | 12 | `build.clj` has a `compile-java` function that compiles the Java code 13 | into `target/classes` and a `run` function that runs the Clojure code 14 | (after compiling the Java code). 15 | 16 | This command compiles the Java code and then runs the Clojure code: 17 | 18 | ```bash 19 | clojure -T:build run 20 | ``` 21 | 22 | You can compile the Java code as a single step with: 23 | 24 | ```bash 25 | clojure -T:build compile-java 26 | ``` 27 | 28 | Once the Java code has been compiled, you can run the Clojure code 29 | with: 30 | 31 | ```bash 32 | clojure -M -m my.main 33 | ``` 34 | -------------------------------------------------------------------------------- /build.clj: -------------------------------------------------------------------------------- 1 | (ns build 2 | (:require [clojure.tools.build.api :as b])) 3 | 4 | (defn compile-java [& _] 5 | (b/delete {:path "target"}) 6 | (b/javac {:src-dirs ["java/src"] 7 | :class-dir "target/classes"})) 8 | 9 | (defn run [& _] 10 | (compile-java) 11 | (let [cmd (b/java-command {:basis (b/create-basis) 12 | :main 'clojure.main 13 | :main-args ["-m" "my.main"]})] 14 | (b/process cmd))) 15 | 16 | (comment 17 | (run) 18 | ) 19 | -------------------------------------------------------------------------------- /clj/src/my/main.clj: -------------------------------------------------------------------------------- 1 | (ns my.main 2 | (:import (my.project Thing))) 3 | 4 | (defn -main [& args] 5 | (println (.stuff (Thing.))) 6 | (shutdown-agents)) 7 | -------------------------------------------------------------------------------- /deps.edn: -------------------------------------------------------------------------------- 1 | {:paths ["clj/src" "target/classes"] 2 | :aliases 3 | {:build 4 | {:deps {io.github.clojure/tools.build {:git/tag "v0.9.4" :git/sha "76b78fe"}} 5 | :ns-default build}}} 6 | -------------------------------------------------------------------------------- /java/src/my/project/Thing.java: -------------------------------------------------------------------------------- 1 | package my.project; 2 | 3 | public class Thing { 4 | public String stuff() { return "From Java!"; } 5 | } 6 | --------------------------------------------------------------------------------