开发者

is it possible for clojure to read and work on classes (.class files) generated by compiler?

is it possible for read and开发者_运维知识库 work on classes (.class files) generated by java code?

e.g.) CalculateSum.java - > CalculateSum.class -> Clojure takes CalculateSum.class and embed it in its code?


You just need to make sure the class file is in your classpath.

Here is some example code.

hello.java:

 public class hello {
     public String sayHi() {
         return "hello world";
     }
 }

$ javac hello.java
$ java -cp clojure.jar:./ clojure.main
user=> (import 'hello) ;; Necessary in 1.2.1, but not 1.3.0
user=> (.sayHi (hello.))
"hello world"


Clojure was designed to embrace its host platform, the JVM. the clojure compiler produces class files that once created behave just like a class file from any other source; so in effect you are already doing this ;)

If you want to use a java class just put an import statement in your namespace:

(ns my.project
  (:import [com.them.library A B C]))

then check out

http://clojure.org/java_interop for what to do next


All of Clojure is pure Java and runs on the JVM. It can load and use any valid Java class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜