java 怎么调用clojure,如何从Java调用Clojure代码

My professor allowed me to practice both Clojure and Java! I'm definitely using the opportunity and want to have my first Java assignment call some Clojure code. Before I go to the actual assignment, I want to create a simple, working example. I need your help.

I have read a few links on Java/Clojure interoperability. This, and This.

I will use the first link to demonstrate what I have done so far:

1) I have created a Clojure Project, dumped the .cli file from the site in it and use the export function in Eclipse to export it as a .jar to a folder in my Documents directory.

2) I have created a second Java Project, dumped the java file into it and added the clojure.jar as a referenced library.

Clojure ns:

(ns com.tiny

(:gen-class

:name com.tiny

:methods [#^{:static true} [binomial [int int] double]]))

Java import:

import com.tiny;

The Java file does not recognize com.tiny. I don't know why. The sites mentioned something about a class-path. So I found the classpath editor in eclipse and added the required folder with the .jar to the list of paths. This also did not work.

I don't know what I'm doing wrong. I have referenced the jar, added it to the classpath, and did a complete copy-paste of the code in the first link (besides the package name).

Java code:

import com.tiny;

public class Main {

public static void main(String[] args) {

System.out.println("(binomial 5 3): " + tiny.binomial(5, 3));

System.out.println("(binomial 10042, 111): " + tiny.binomial(10042, 111));

}

}

Clojure code:

(ns com.tiny

(:gen-class

:name com.tiny

:methods [#^{:static true} [binomial [int int] double]]))

(defn binomial

"Calculate the binomial coefficient."

[n k]

(let [a (inc n)]

(loop [b 1

c 1]

(if (> b k)

c

(recur (inc b) (* (/ (- a b) b) c))))))

(defn -binomial

"A Java-callable wrapper around the 'binomial' function."

[n k]

(binomial n k))

(defn -main []

(println (str "(binomial 5 3): " (binomial 5 3)))

(println (str "(binomial 10042 111): " (binomial 10042 111)))

)

解决方案

To successfully get your example to run, I did the following:

1) Created a Clojure project in Eclipse

2) Added the file tiny.clj in src/com/tiny.clj

3) Exported that project as a .jar file (e.g.: tiny.jar)

4) Created the Java project as you describe, with one Java file - Main.java

5) Add clojure.jar and tiny.jar to the Java project's classpath. (Right-click -> Configure Build Path -> Libraries Tab -> Add External JARs)

When I initially tried this, I didn't put the tiny.clj file in the com/ directory (I had it directly under src/), and I got a RuntimeException when I tried to run the Main class that said the file tiny.clj and tiny__init.class couldn't be found. Moving the tiny.clj file to the com/ subdirectory fixed the issue for me and Main rain successfully.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值