Search code examples
clojureclasspathrequireenvironment

FileNotFoundException when requiring namespaces in Clojure


I want to use clojure.java.io and clojure.data.csv in my app.

This is how I've required those namespaces:

(ns my-app.core
  (:gen-class)
  (:require [clojure.java.io :as io]
            [clojure.data.csv :as csv]))

When I evaluate that ns function in the REPL, I get this error:

; Execution error (FileNotFoundException) at my-app.core/eval8295$loading (form-init1647489175359256060.clj:1).
; Could not locate clojure/data/csv__init.class, clojure/data/csv.clj or clojure/data/csv.cljc on classpath.

VS Code v1.62.3 / Calva. v2.0.229 / Leiningen v2.9.8 on Java v15.0.1 / macOS v11.6.1

This is my Java class path, as produced with within the same REPL session by calling (java.lang.System/getProperty "java.class.path"):

"/Users/my-username/Code/my-app/test:/Users/my-username/Code/my-app/src:/Users/my-username/Code/my-app/dev-resources:/Users/my-username/Code/my-app/resources:/Users/my-username/Code/my-app/target/default/classes:/Users/my-username/.m2/repository/cider/cider-nrepl/0.22.4/cider-nrepl-0.22.4.jar:/Users/my-username/.m2/repository/nrepl/nrepl/0.8.3/nrepl-0.8.3.jar:/Users/my-username/.m2/repository/org/clojure/clojure/1.10.3/clojure-1.10.3.jar:/Users/my-username/.m2/repository/org/clojure/spec.alpha/0.2.194/spec.alpha-0.2.194.jar:/Users/my-username/.m2/repository/org/clojure/core.specs.alpha/0.2.56/core.specs.alpha-0.2.56.jar:/Users/my-username/.m2/repository/org/nrepl/incomplete/0.1.0/incomplete-0.1.0.jar"

And this is my app directory structure (scaffolded with lein new app my-app):

.
├── CHANGELOG.md
├── LICENSE
├── README.md
├── doc
│   └── intro.md
├── project.clj
├── resources
│   ├── my-data-1.csv
│   └── my-data-2.csv
├── src
│   └── my_app
│       └── core.clj
├── target
│   └── default
│       ├── classes
│       │   └── META-INF
│       │       └── maven
│       │           └── my-app
│       │               └── my-app
│       │                   └── pom.properties
│       ├── repl-port
│       └── stale
│           └── leiningen.core.classpath.extract-native-dependencies
└── test
    └── my_app
        └── core_test.clj

It looks like something is wrong with my Java environment, or perhaps my Clojure installation. I can't seem to figure out where the problem lies, and how to go about fixing it.

Any advice would be greatly appreciated.


Solution

  • I had neglected to add clojure.data.csv to my project.clj file.

    Here is the project.clj with the changes applied to :dependencies:

    (defproject my-app "0.1.0-SNAPSHOT"
      :description "FIXME: write description"
      :url "http://example.com/FIXME"
      :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
                :url "https://www.eclipse.org/legal/epl-2.0/"}
      :dependencies [[org.clojure/clojure "1.10.3"]
                     [org.clojure/data.csv "1.0.0"]]
      :main ^:skip-aot my-app.core
      :target-path "target/%s"
      :profiles {:uberjar {:aot :all
                           :jvm-opts ["-Dclojure.compiler.direct-linking=true"]}})
    

    Relevant part:

    :dependencies [[org.clojure/clojure "1.10.3"]
                   [org.clojure/data.csv "1.0.0"]]
    

    Now it works fine.