简体   繁体   中英

Requiring java classes and namespace organization with local maven repository in Clojure

So I think I understand requiring and importing namespaces in general, for example the Clojure source code has this directory structure: clojure / src / clj / clojure / java / io.clj . Which means I can require things like this:

user> (ns foo
        (:require [clojure.java.io :as io]))
nil
foo> io/copy
#<io$copy clojure.java.io$copy@35f784d7>

I interpret this as there is a function called copy in the io.clj file which is in this namespace clojure.java.io, which I can now use as I required that namespace.

Now to my specific question. I need to use a library, but the jar isn't on maven, so I set up a local repository in the standard way and it works fine. So I now have the needed jars in two places:

/Users/ryanmoore/projects/proj_foo/maven_repo/net/sf/picard/1.86

and

/Users/ryanmoore/.m2/repository/net/sf/picard/1.86

The problem is inside the jar the classes have names such as

net/sf/picard/illumina/parser/readers/MMapBackedIteratorFactory.class
net/sf/picard/io/FastLineReader.class
net/sf/picard/metrics/MetricsFile.class
net/sf/picard/reference/FastaSequenceIndexEntry.class

I am having trouble requiring these classes eg:

user> (require '[net.sf.picard.io/FastLineReader :as flr])
FileNotFoundException Could not locate FastLineReader__init.class or FastLineReader.clj on      classpath:   clojure.lang.RT.load (RT.java:432)

and I think that it has something to do with the names and paths of the not matching up. In this stack question What are common conventions for using namespaces in Clojure? , it states that files in directory structure must match up with the namesapces or Clojure won't be able to find them.

My question is how should I set up the folders so that Clojure can find everything?

Note: There is a version of picardtools on Clojars, but it is an old version. Plus I tried that to and was having the same sort of problems as above.

Edit: Thanks to mobyte's answer, I now get what "Java classes get imported, not required." from here How to require java classes in clojure/leiningen is referring to.

You need to use import with Java classes

(import '[net.sf.picard.io FastLineReader])

require is for Clojure libs: http://clojuredocs.org/clojure_core/clojure.core/require .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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