liveBook · Manning

Clojure.java.io/resource

As explained in Clojure: The Essential Reference

clojure.java.io/resource is a Clojure function that retrieves a URL for a resource located on the Java classpath.

Use clojure.java.io/resource to locate a resource on the Java classpath and obtain a URL that can feed Clojure I/O functions. section 22.8

Read a classpath resource

After requiring resource and reader, (def cjio (resource "clojure/java/io.clj")) retrieves the resource URL. Passing cjio to reader and line-seq makes the first line available, producing "; Copyright (c) Rich Hickey. All rights reserved." section 22.8

(require '[clojure.java.io :refer [resource reader]])

(def cjio (resource "clojure/java/io.clj")) ; #1

(first (line-seq (reader cjio))) ; #2
;; ";   Copyright (c) Rich Hickey. All rights reserved."

Inspect the complete executable example showing classpath lookup followed by reading the returned URL. section 22.8

How Clojure: The Essential Reference covers it

How deep it goes
Demonstrates lookup and subsequent reading, but does not detail missing-resource behavior or implementation internals.
Examples
Uses clojure/java/io.clj and prints its copyright line after reading the returned URL.
What it assumes
Assumes basic Clojure requiring and familiarity with readers and line-seq.
What you can do after
Apply it via clojure.java.io/reader to read a classpath resource line by line.
How it connects
Connects classpath resources with URLs, readers, and line-seq.

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.