Fetching contributors…
Cannot retrieve contributors at this time
98 lines (83 sloc) 3.16 KB
;;; classpath.clj: utilities for working with the Java class path
;; by Stuart Sierra,
;; April 19, 2009
;; Copyright (c) Rich Hickey, Stuart Sierra, and contributors.
;; All rights reserved. The use
;; and distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (
;; which can be found in the file epl-v10.html at the root of this
;; distribution. By using this software in any fashion, you are
;; agreeing to be bound by the terms of this license. You must not
;; remove this notice, or any other, from this software.
^{:author "Stuart Sierra"
:doc "Utilities for dealing with the JVM's classpath"}
(:require [ :as io])
(:import ( File)
(java.util.jar JarFile JarEntry)
( URL URLClassLoader)))
(defprotocol URLClasspath
(urls [loader]
"Returns a sequence of objects representing locations
which this classloader will search for classes and resources."))
(urls [loader] (seq (.getURLs loader))))
(defn get-urls
"Returns a sequence of objects used by this
classloader, or nil if the classloader does not sastify the
URLClasspath protocol."
(when (satisfies? URLClasspath loader)
(urls loader)))
(defn jar-file?
"Returns true if file is a normal file with a .jar or .JAR extension."
(let [file (io/file f)]
(and (.isFile file)
(or (.endsWith (.getName file) ".jar")
(.endsWith (.getName file) ".JAR")))))
(defn filenames-in-jar
"Returns a sequence of Strings naming the non-directory entries in
the JAR file."
[^JarFile jar-file]
(map #(.getName ^JarEntry %)
(filter #(not (.isDirectory ^JarEntry %))
(enumeration-seq (.entries jar-file)))))
(defn system-classpath
"Returns a sequence of File paths from the 'java.class.path' system
(map #(File. ^String %)
(.split (System/getProperty "java.class.path")
(System/getProperty "path.separator"))))
(defn loader-classpath
"Returns a sequence of File paths from a classloader."
(map io/as-file (get-urls loader)))
(defn classpath
"Returns a sequence of File objects of the elements on the
classpath. Defaults to searching for instances of in the classloader hierarchy above
clojure.lang.RT/baseLoader or the given classloader. If no
URLClassloader can be found, as on Java 9, falls back to the
'java.class'path' system property."
(iterate #(.getParent ^ClassLoader %) classloader)))))
(or (seq (classpath (clojure.lang.RT/baseLoader)))
(defn classpath-directories
"Returns a sequence of File objects for the directories on classpath."
(filter #(.isDirectory ^File %) (classpath)))
(defn classpath-jarfiles
"Returns a sequence of JarFile objects for the JAR files on classpath."
(map #(JarFile. ^File %) (filter jar-file? (classpath))))