简体   繁体   English

使用第三方java库,如com.jcraft.jsch,使用clojure

[英]Using 3rd party java libraries, like com.jcraft.jsch, with clojure

I'm experimenting with clojure and am trying to get a feel for using 3rd party libraries. 我正在尝试使用clojure,并尝试使用第三方库。 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. 我已经能够下载一些源代码,将它捆绑到一个带有leiningen的jar文件中,把它放在我的类路径中,并在我的脚本中使用“lib.etc”。 I've also played around with the objects in java.lang.*. 我也玩过java.lang。*中的对象。

I haven't had any success with 3rd party java, though. 不过,我对第三方java没有任何成功。

$ 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)"
$

It looks like an __init.class or .clj file must be created. 看起来必须创建__init.class或.clj文件。 Is this true, or is there some alternative way that pure java classes are supposed to be loaded? 这是真的,还是有一些替代方法可以加载纯java类?

For java classes use import : 对于java类,使用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

Note the package and class names do not need to be quoted since import is a macro. 请注意,不需要引用包名和类名,因为import是一个宏。

Also there is no equivalent to import java.util.*; 也没有相同的import java.util.*; You need to specify which classes you want to import. 您需要指定要导入的类。

尝试使用import进行非clojure的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM