Packages

So far we’ve been using the fully qualified classnames for files: java.io.File and java.awt.Rectangle. Since that can be a bit of a mouthful, Clojure provides import. In much the same way that you can use require/:as to create syntactical shortcuts for the denizens of your Clojure namespaces, you can use import to do the same with Java classes. In the REPL you can use the stand-alone form of import:

 ;; In the REPL.
 
 (import java.io.File)

If you are writing a .clj file you should include the import in the ns at the top:

 (ns read-authors
  (:import java.io.File))

Note that there is no quoting needed in either form. However you do it, once you have imported a class you can refer to the class without its package name:

 (​def​ authors ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.