ClojureDocs

Nav

Namespaces

load-file

clojure.core

Available since 1.0
  • (load-file name)
Sequentially read and evaluate the set of forms contained in the file.
3 Examples
;; Very useful from a REPL
;; Paths are specified as strings using canonical file path notation 
;; (rather than clojure-style namespaces dependent on the JVM classpath).
;; The working directory is set to wherever you invoked the JVM from, 
;; likely the project root.

(load-file "src/mylib/core.clj")

;; now you can go and evaluate vars defined in that file.
;; file located at src/address_book/core.clj
;; current dir is src/..

(load-file "src/address_book/core.clj")
;; create a clojure file on the fly using spit
;; then load it into the REPL and use its function
;; notice the return value is var quote of the last form of file

user=> (spit "mycode.clj" "(defn doub [x] (* x 2))")
nil
user=> (load-file "mycode.clj")
#'user/doub
user=> (doub 23)
46

;; Note this is similar to using load-string:
user=> (load-string "(defn doub [x] (* x 2))")
#'user/doub
See Also

Loads Clojure code from resources in classpath. A path is interpreted as classpath-relative if it ...

Added by gstamp

Opposite of slurp. Opens f with writer, writes content, then closes f. Options passed to clojure....

Added by AtKaaZ

Sequentially read and evaluate the set of forms contained in the string

Added by witek
2 Notes
    By , created 12.5 years ago

    Be aware that this function is intended to load code only. If your data structures or a string in them grow bigger than around 65,535 it crashes.

    Exception similar to:

    java.lang.ClassFormatError: Unknown constant tag 49 in class file parse$eval13

    Please use read-string instead.

    Example:

    (read-string (slurp "data.clj"))

    Source: Google Groups

    By , created 11.8 years ago, updated 11.8 years ago

    The following marginally helpful error will be thrown if you have a typo in your file:

    CompilerException java.lang.RuntimeException: Unable to resolve symbol: load-file in this context, compiling:(NO_SOURCE_PATH:1:1)

    Fix the syntax error(s) in and you'll be able to use load-file again.