Search code examples
clojure

How does Clojure find sources in the classpath?


Libraries in Clojure are packed the in jars the same as Java.
But these jars contain source code to be compiled, so, in theory, Clojure needs to check every file inside every jar in the classpath to see if it's a Clojure source file (sounds inefficient), specially if the code needs to be AOT compiled.

Does Clojure actually do this or does it have some heuristic to find out which jars contain .clj files?


Solution

  • To access Clojure code, first you have to require the namespace. When require is called it will derive the source file name from the namespace name.

    From the Clojure documentation of require:

    A lib's name also locates its root directory within classpath using Java's package name to classpath-relative path mapping.

    [...]

    The root resource path is derived from the lib name in the following manner: Consider a lib named by the symbol 'x.y.z; it has the root directory /x/y/, and its root resource is /x/y/z.clj, or /x/y/z.cljc if /x/y/z.clj does not exist.