java 怎么调用clojure_java 调用 clojure

这里不介绍调用文本clojure然后jvm中执行的过程, 也不介绍手动aot 来编译成class来给jvm调用。主要还是介绍下使用lein来打包成jar 然后java程序中调用,本人觉得这个是最舒服的做法。

新建一个工程

$ lein new app hello

直接运行测试

$ lein run

Hello, World!

编辑文件 src/hello/core.clj

(ns hello.t

(:gen-class

:methods [#^{:static true} [jerry [String] String]])

)

(defn -jerry [gophee]

(str "jerry like tom !" gophee)

)

注意这里可能会遇到一个问题,默认的lein生成的project的project.clj是

(defproject my-stuff "0.1.0-SNAPSHOT"

:description "FIXME: write description"

:url "http://example.com/FIXME"

:license {:name "Eclipse Public License"

:url "http://www.eclipse.org/legal/epl-v10.html"}

:dependencies [[org.clojure/clojure "1.5.1"]]

:main ^:skip-aot hello.core

:target-path "target/%s"

:profiles {:uberjar {:aot :all}})

打包后发现只有main的才生成name space的,可以修改成

(defproject hello "0.1.0-SNAPSHOT"

:description "FIXME: write description"

:url "http://example.com/FIXME"

:license {:name "Eclipse Public License"

:url "http://www.eclipse.org/legal/epl-v10.html"}

:dependencies [[org.clojure/clojure "1.5.1"]]

;:main ^:skip-aot hello.core

:main librarian-clojure.run

:aot :all

:target-path "target/%s"

:profiles {:uberjar {:aot :all}})

当然也可以用正则表达式来定义name space

You can also specify regex as parameter for :compile option – it will compile only matched namespaces

可以参考

http://blog.japila.pl/2012/02/aot-compile-all-namespaces-in-a-clojure-project-aot-all-in-project-clj-leiningen/

打包到jar

$ lein uberjar

0818b9ca8b590ca3270a3433284dd417.png

然后在eclipse 中添加hello-0.1.0-SNAPSHOT.jar

写个java类测试下

public class CallClass {

public static void main(String[] args) {

String  ss = hello.t.jerry(" test ");

System.out.println(ss);

}

}

输出 jerry like  tom ! test

ok,完工

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值