load-file

clojure.core

  • (load-file name)
Sequentially read and evaluate the set of forms contained in the file.

3 Examples top

  • ;; 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
    
    user=> (spit "mycode.clj" "(defn doub [x] (* x 2))")
    nil
    user=> (load-file "mycode.clj")
    #'user/doub
    user=> (doub 23)
    46
    user=>
Log in to add / edit an example.

See Also top

  • 0
    clojure.core/load

    Loads Clojure code from resources in classpath. A path is interpreted

  • 0
    clojure.core/spit

    Opposite of slurp. Opens f with writer, writes content, then close

Log in to add a see also.

Comments top

2 comment(s) for load-file.

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

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.

Log in to add a comment.