如何从Java调用Clojure代码 [英] How to Call Clojure Code From Java

查看:204
本文介绍了如何从Java调用Clojure代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的教授允许我练习Clojure和Java!我肯定是在利用这个机会,并希望让我的第一个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.

我已经阅读了一些关于Java / Clojure互操作性的链接。 ,以及这个

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)我创建了一个Clojure项目,从站点中转储.cli文件并使用Eclipse中的导出函数将其导出为a .jar到我的Documents目录中的一个文件夹。

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)我创建了第二个Java项目,将java文件转储到其中并添加了clojure.jar作为参考图书馆。

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:

Clojure ns:

(ns com.tiny
  (:gen-class
   :name com.tiny
   :methods [#^{:static true} [binomial [int int] double]]))

Java导入:

import com.tiny;

Java文件无法识别 com.tiny 。我不知道为什么。这些网站提到了一些类路径。所以我在eclipse中找到了类路径编辑器,并将带有.jar的必需文件夹添加到路径列表中。这也行不通。

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.

我不知道我做错了什么。我引用了jar,将它添加到类路径中,并在第一个链接中完成了代码的完整复制粘贴(除了包名称)。

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代码:

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代码:

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)在Eclipse中创建了一个Clojure项目
2)在src / com / tiny.clj
3)Expo中添加了文件tiny.clj将该项目作为.jar文件(例如:tiny.jar)

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)使用一个Java文件创建Java项目 - Main.java
5)将clojure.jar和tiny.jar添加到Java项目的类路径中。 (右键单击 - >配置构建路径 - >库选项卡 - >添加外部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)

当我最初尝试这个时,我没有放下tiny.clj文件在com /目录中(我直接在src /下),当我试图运行Main类时,我得到一个RuntimeException,表示找不到文件tiny.clj和tiny__init.class。将tiny.clj文件移动到com /子目录,为我和主要雨成功解决了问题。

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.

这篇关于如何从Java调用Clojure代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆