使用第三方Java库,如com.jcraft.jsch,使用clojure [英] Using 3rd party java libraries, like com.jcraft.jsch, with clojure
问题描述
我正在尝试 clojure ,并试图获得使用第三方库的感觉。我已经能够下载一些源,捆绑到一个jar文件与leiningen,把它放在我的类路径和(使用lib.etc)在我的脚本。我也玩过java.lang。*中的对象。
I'm experimenting with clojure and am trying to get a feel for using 3rd party libraries. I've been able to download some source, bundle it into a jar file with leiningen, put it in my classpath and (use 'lib.etc) in my script. I've also played around with the objects in java.lang.*.
我没有在第三方Java上取得任何成功。
I haven't had any success with 3rd party java, though.
$ java -cp clojure.jar:clojure-contrib.jar:com.jcraft.jsch_0.1.31.jar clojure.main
Clojure 1.1.0
user=> (require 'com.jcraft.jsch)
java.io.FileNotFoundException: Could not locate com/jcraft/jsch__init.class or com/jcraft/jsch.clj on classpath: (NO_SOURCE_FILE:0)
$ jar tf com.jcraft.jsch_0.1.31.jar | egrep "(init|clj)"
$
看起来像一个__init.class .clj文件必须创建。
It looks like an __init.class or .clj file must be created. Is this true, or is there some alternative way that pure java classes are supposed to be loaded?
推荐答案
对于java类,使用 import
:
For java classes use import
:
(import java.util.ArrayList)
;// or use a prefix for multiple classes:
(import [java.util ArrayList Collection])
;// or preferably in the ns declaration:
(ns my.lib
[:import [java.util ArrayList Collection]])
user=> (def al (ArrayList.))
#'user/al
user=> (.add al "hi")
true
user=> (.size al)
1
注意包和类名不需要引用自 import
是一个宏。
Note the package and class names do not need to be quoted since import
is a macro.
也不等同于 import java。 util。*;
您需要指定要导入哪些类。
Also there is no equivalent to import java.util.*;
You need to specify which classes you want to import.
这篇关于使用第三方Java库,如com.jcraft.jsch,使用clojure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!