Calling clojure from java

前端 未结 9 1403
梦毁少年i
梦毁少年i 2020-11-22 11:13

Most of the top google hits for \"calling clojure from java\" are outdated and recommend using clojure.lang.RT to compile the source code. Could you help with a

9条回答
  •  粉色の甜心
    2020-11-22 11:47

    I agree with clartaq's answer, but I felt that beginners could also use:

    • step-by-step information on how to actually get this running
    • information that's current for Clojure 1.3 and recent versions of leiningen.
    • a Clojure jar that also includes a main function, so it can be run standalone or linked as a library.

    So I covered all that in this blog post.

    The Clojure code looks like this:

    (ns ThingOne.core
     (:gen-class
        :methods [#^{:static true} [foo [int] void]]))
    
    (defn -foo [i] (println "Hello from Clojure. My input was " i))
    
    (defn -main [] (println "Hello from Clojure -main." ))
    

    The leiningen 1.7.1 project setup looks like this:

    (defproject ThingOne "1.0.0-SNAPSHOT"
      :description "Hello, Clojure"
      :dependencies [[org.clojure/clojure "1.3.0"]]
      :aot [ThingOne.core]
      :main ThingOne.core)
    

    The Java code looks like this:

    import ThingOne.*;
    
    class HelloJava {
        public static void main(String[] args) {
            System.out.println("Hello from Java!");
            core.foo (12345);
        }
    }
    

    Or you can also get all the code from this project on github.

提交回复
热议问题