Using third-party java libraries like com.jcraft.jsch with clojure

I am experimenting with clojure and trying to figure out how to use third-party libraries. I was able to load some source, link it in a jar file with leiningen, put it in my classpath and (use 'lib.etc) in my script. I also played with objects in java.lang. *.

I had no success with third-party java, however.

$ 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 the __init.class or .clj file should be created. Is this true or is there an alternative way that pure Java classes should be loaded?

+7
java libraries clojure jsch
source share
2 answers

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 that package and class names do not have to be specified, since import is a macro.

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

+8
source share

Try using import for clojure material.

+1
source share

All Articles