Kenny Evitt
Kenny Evitt

Reputation: 9811

Why are none of the folders in my Clojure classpath considered directories and why do none of the files exist?

Below is the code I'm running from a REPL (in Light Table) about which I'm currently confused.

This seems to work fine:

(require '[clojure.java.classpath :as classpath])

(import '[java.io File])

(map #(File. (.toURI %))
  (classpath/classpath))

Results:

(#<File C:\Documents%20and%20Settings\Kenny\My%20Documents\hello-heroku-clojure-world\test>
 #<File C:\Documents%20and%20Settings\Kenny\My%20Documents\hello-heroku-clojure-world\src>
 #<File C:\Documents%20and%20Settings\Kenny\My%20Documents\hello-heroku-clojure-world\dev-resources>
 #<File C:\Documents%20and%20Settings\Kenny\My%20Documents\hello-heroku-clojure-world\resources>
 #<File C:\Documents%20and%20Settings\Kenny\My%20Documents\hello-heroku-clojure-world\target\classes>
...

But then the following seems to indicate that none of the files are directories:

(filter #(.isDirectory (File. (.toURI %)))
  (classpath/classpath))

Results:

()

EDIT – I'm pretty sure the following is a simpler version of the code about which I'm confused; it produces the same results:

(filter #(.isDirectory %)
  (classpath/classpath))

However, this seems to work:

(filter #(.isDirectory (File. (URLDecoder/decode (.getPath %))))
  (classpath/classpath))

Results:

(#<File C:\Documents%20and%20Settings\Kenny\My%20Documents\hello-heroku-clojure-world\test>
 #<File C:\Documents%20and%20Settings\Kenny\My%20Documents\hello-heroku-clojure-world\src>
 #<File C:\Documents%20and%20Settings\Kenny\My%20Documents\hello-heroku-clojure-world\resources>
 #<File C:\Documents%20and%20Settings\Kenny\My%20Documents\hello-heroku-clojure-world\target\classes>)

[All of the other files in my classpath are JAR files.]

EDIT – I'm running this on Windows XP.

EDIT – surely this is an issue with paths and spaces; I ran the following:

(filter #(.exists %)
  (classpath/classpath))

Results:

()

The relevant code from clojure.java.classpath:

(defn loader-classpath
  "Returns a sequence of File paths from a classloader."
  [loader]
  (when (instance? java.net.URLClassLoader loader)
    (map io/as-file (.getURLs ^java.net.URLClassLoader loader))))

Upvotes: 0

Views: 143

Answers (1)

Jared314
Jared314

Reputation: 5231

The issue is with the spaces in your paths. This was fixed in version 0.2.1, but the readme was not updated, and still says 0.2.0.

Use [org.clojure/java.classpath "0.2.1"] in your project.clj :dependencies vector.

Note: The clojure.java.classpath/classpath-directories function will filter the directories for you.

Upvotes: 3

Related Questions