将 3rd 方 Java 库(例如 com.jcraft.jsch)与 clojure 结合使用

发布于 2024-09-11 10:27:04 字数 644 浏览 3 评论 0原文

我正在尝试 clojure 并试图了解如何使用第 3 方库。我已经能够下载一些源代码,使用 leiningen 将其捆绑到 jar 文件中,将其放入我的类路径中并在我的脚本中(使用“lib.etc”)。我还尝试过 java.lang.* 中的对象。

不过,我在 3rd party 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)"
$

看起来必须创建 __init.class 或 .clj 文件。这是真的,还是有一些替代方法来加载纯java类?

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.*.

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光下慵懒的猫 2024-09-18 10:27:04

对于 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

请注意,包名和类名不需要加引号,因为 import 是一个宏。

此外,没有与 import java.util.*; 等效的功能,您需要指定要导入的类。

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

Note the package and class names do not need to be quoted since import is a macro.

Also there is no equivalent to import java.util.*; You need to specify which classes you want to import.

む无字情书 2024-09-18 10:27:04

尝试使用 import 处理非 Clojure 内容。

Try using import for non-clojure stuff.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文