1

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.

Community
  • 1
  • 1

1 Answers1

1

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.

mobyte
  • 3,752
  • 1
  • 18
  • 16
  • Thanks! So this imports it into the namespace: (ns bar (:import (net.sf.picard.io FastLineReader))), but I didn't see anything in the clojure docs for import about changing the name to a shorter one...is there a way? –  Mar 07 '13 at 01:41
  • @RyanMoore You already can use classname without ns prefix. Afaik there is no mechanism which allows you change the name of importing Java class. – mobyte Mar 07 '13 at 03:39