调试在java代码(eclipse)中调用的clojure函数 [英] Debugging clojure functions which are invoked in java code (eclipse)

查看:118
本文介绍了调试在java代码(eclipse)中调用的clojure函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下链接从java调用clojure ,它说明如何编写一段clojure代码,其功能我们可以直接在java源代码中调用。为了重现,我们有一个名为tiny的clojure项目,其中有一个tiny.clj源文件。 tiny.clj文件包含以下代码:

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


计算二项式系数
[nk]
(let [a(inc n)]
(loop [b 1
c 1]
(if(> bk)
c
(recur(inc b)(*(/( - ab)b)c))))))

(defn-binomial
一个Java可调用的包装器,围绕二项式函数。
[nk]
(二项式nk))

(defn -main []
(println(str(binomial 5 3):(binomial 5 3)))
(println(str(binister 10042 111):(binomial 10042 111)))

然后将其导出到ttt.jar文件中,然后将其添加到Referenced Libraries tinyJava项目(这是一个java项目)。在tinyJava项目中有一个Main.java文件,它有以下代码:

  import com.domain.tiny; 

public class Main {

public static void main(String [] args){

int j;
j =(int)tiny.binomial(5,3);
System.out.println((binomial 5 3):+ j);
System.out.println((binister 10042,111):+ tiny.binomial(10042,111));

}

}

那么:

 (二项式5 3):10 
(二项式10042,111):4.9068389575068143E263

我的问题是,在Main.java中的以下一点:

  j =(int)tiny.binomial(5,3); 

是否可以进入clojure源代码?我试过,它没有工作。



感谢



:我将jar添加到Referenced Libraries的方式如下: / p>

1)右键单击项目tinyJava&选择属性



2)选择:Java构建路径



3)选择:库



4)点击:添加外部Jars



5)然后转到ttt.jar的位置



:对于scala项目,似乎可以以稍微不同的方式实现这一点(见下文),从而链接项目而不是显式地导出jar文件。例如,这似乎是很容易做Scala(注意下面的文章还讨论了interop: ://stackoverflow.com/questions/1179406/how-do-you-call-scala-objects-from-java>如何从Java调用Scala对象?)。



我所做的如下:



1)设置一个名为firstScala的Scala项目,文件MyPrinter.scala与以下代码:

  class MyPrinter {
def printTerm ){
print(hello);
}
}



2)然后我创建了一个名为firstScalaJava的Java项目,然后我用下面的代码添加一个名为Main.java的文件:

 class Main { 
public static void main(String args []){
MyPrinter myPrint = new MyPrinter();
myPrint.printTerm();
}
}

3)然后我右键点击firstScalaJava, ,选择Java Build Path,选择Projects选项卡,点击Add,然后选择firstScala项目。



4)然后如果我在myPrint.printTerm ();并在调试器此时停止时点击F5,调试器自动进入Scala源文件。



这个好处是我不需要导出任何jar文件或任何类似的东西。这是有吸引力的,因为在更改Scala代码时,不需要做任何事情(eclipse自动重建scala项目时,试图运行java项目),而如果我改变Clojure代码,我需要重新导出jar etc ...(注意,以上述方式链接项目不适用于clojure情况)


At the following link Calling clojure from java , it is illustrated how to write a piece of clojure code, whose functionality we can then invoke directly in java source code. To reproduce, we have a clojure project called tiny, which within it has a tiny.clj source file. The tiny.clj file contains the following code :

(ns tiny
  (:gen-class
    :name com.domain.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)))
)

This is then exported into a ttt.jar file, which is then added to the "Referenced Libraries" of the tinyJava project (which is a java project). Inside the tinyJava project there is a Main.java file, which has the following code :

import com.domain.tiny;

public class Main {

    public static void main(String[] args) {

        int j;
        j = (int)tiny.binomial(5, 3);
        System.out.println("(binomial 5 3): " + j);
        System.out.println("(binomial 10042, 111): " + tiny.binomial(10042, 111));

    }

}

The output is then :

(binomial 5 3): 10
(binomial 10042, 111): 4.9068389575068143E263

My question is, at the following point in Main.java :

j = (int)tiny.binomial(5, 3);

is it possible to step into the clojure source code? I have tried and it did not work.

Thanks

[EDIT] : the way I added the jar to the Referenced Libraries was as follows :

1)Rightclick on project tinyJava & choose properties

2)Choose : Java Build Path

3)Choose : Libraries

4)Click : "Add External Jars"

5)Then go to the location of ttt.jar

[EDIT 2] : For a scala project it seems that one can achieve this in a slightly different manner (see below), whereby one links the projects rather than explicitly exporting a jar file.

解决方案

For example this seems to be quite easy to do with Scala (note the following article also discusses interop : How do you call Scala objects from Java?).

What I did was as follows :

1)Set up a Scala project called firstScala, and add a file MyPrinter.scala with the following code :

class MyPrinter{ 
  def printTerm() {
        print("hello");
  }
}

2)Then I created a Java project called firstScalaJava, to which I then added a file called Main.java with the following code :

class Main {
    public static void main(String args[]) {
        MyPrinter myPrint = new MyPrinter();
        myPrint.printTerm();
    }
}

3)Then I right clicked on firstScalaJava, selected Properties, chose Java Build Path, chose Projects tab, clicked "Add", and then selected the firstScala project.

4)Then if I put a breakpoint at "myPrint.printTerm();" and clicked F5 when the debugger has stopped at this point, the debugger automatically walks into the Scala source file.

The nice thing about this is that I did not need to export any jar files or anything like that. This is appealing because upon changing the Scala code one does not need to do anything (eclipse automatically rebuilds the scala project when one attempts to run the java project thereafter), whereas if I changed the Clojure code I would need to re-export the jar etc... (note that linking the projects in the manner described above does not work for the clojure case)

这篇关于调试在java代码(eclipse)中调用的clojure函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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