English 中文(简体)
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. 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?

最佳回答

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.

问题回答

Try using import for non-clojure stuff.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签