Use both java.class.path and Clojure's URLClassLoader
Stuart Sierra
12 years ago
18 | 18 | clojure.java.classpath |
19 | 19 | (:require [clojure.java.io :as io]) |
20 | 20 | (:import (java.io File) |
21 | (java.util.jar JarFile JarEntry))) | |
21 | (java.util.jar JarFile JarEntry) | |
22 | (java.net URL URLClassLoader))) | |
22 | 23 | |
23 | 24 | (set! *warn-on-reflection* true) |
24 | 25 | |
38 | 39 | (filter #(not (.isDirectory ^JarEntry %)) |
39 | 40 | (enumeration-seq (.entries jar-file))))) |
40 | 41 | |
41 | (defn classpath | |
42 | "Returns a sequence of File objects of the elements on CLASSPATH." | |
42 | (defn system-classpath | |
43 | "Returns a sequence of File paths from the 'java.class.path' system | |
44 | property." | |
43 | 45 | [] |
44 | 46 | (map #(File. ^String %) |
45 | 47 | (.split (System/getProperty "java.class.path") |
46 | 48 | (System/getProperty "path.separator")))) |
49 | ||
50 | (defn clojure-classpath | |
51 | "Returns a sequence of File paths from Clojure's base classloader." | |
52 | [] | |
53 | (let [loader (clojure.lang.RT/baseLoader)] | |
54 | (when (instance? URLClassLoader loader) | |
55 | (map #(File. (.getPath ^URL %)) (.getURLs ^URLClassLoader loader))))) | |
56 | ||
57 | (defn classpath | |
58 | "Returns a sequence of File objects of the elements on the | |
59 | classpath." | |
60 | [] | |
61 | (distinct (concat (clojure-classpath) (system-classpath)))) | |
47 | 62 | |
48 | 63 | (defn classpath-directories |
49 | 64 | "Returns a sequence of File objects for the directories on classpath." |