Clojure项目导入cheshire库失败求助:类路径找不到文件
解决Clojure中cheshire依赖无法加载的问题
问题代码
业务代码(problem2.clj)
(ns problem2 (:require [cheshire.core :as json] [clojure.spec.alpha :as s] [invoice-spec])) (defn json->map [file-name] (-> file-name slurp json/parse-stream :invoice)) (defn generate-invoice [file-name] (let [invoice (json->map file-name) customer {:name (:company_name (:customer invoice)) :email (:email (:customer invoice))} items (map (fn [item] {:price (:price item) :quantity (:quantity item) :sku (:sku item) :taxes (map (fn [tax] {:category (keyword (:tax_category tax)) :rate (:tax_rate tax)}) (:taxes item))}) (:items invoice))] {:issue-date (java.util.Date. (:issue_date invoice)) :customer customer :items items})) (defn -main [] (let [invoice (generate-invoice "invoice.json")] (if (s/valid? ::invoice-spec/invoice invoice) (println "Invoice is valid") (println "Invoice is not valid"))))
依赖配置(deps.edn)
{:deps {org.clojure/data.json {:mvn/version "2.4.0"} org.clojure/clojure {:mvn/version "1.10.3"} org.clojure/spec.alpha {:mvn/version "0.2.194"} cheshire {:mvn/version "5.10.0"}} }
错误信息
Execution error (FileNotFoundException) at problem2/eval138$loading (problem2.clj:1). Could not locate cheshire/core__init.class, cheshire/core.clj or cheshire/core.cljc on classpath.
解决方案
1. 修复deps.edn语法错误
你的deps.edn末尾多了一个多余的闭合大括号,导致Clojure工具无法正确解析依赖配置。修正后的deps.edn如下:
{:deps {org.clojure/data.json {:mvn/version "2.4.0"} org.clojure/clojure {:mvn/version "1.10.3"} org.clojure/spec.alpha {:mvn/version "0.2.194"} cheshire {:mvn/version "5.10.0"}}}
2. 清理缓存并重新加载依赖
运行以下命令清除本地依赖缓存,确保新的配置生效:
clj -Sclean
之后重新启动你的Clojure运行环境(REPL或项目进程)。
3. 验证依赖加载
在REPL中执行以下代码,确认cheshire依赖已正确加载:
(require '[cheshire.core :as json])
如果没有抛出异常,说明依赖加载成功,此时重新运行你的-main函数即可。
额外检查
确保你的problem2.clj文件位于正确的目录下:
- 如果直接放在项目根目录,运行时使用
clj -m problem2启动 - 如果放在
src目录下,默认clj会自动将src纳入类路径,无需额外配置
内容的提问来源于stack exchange,提问作者Luis Carlos Castillo Martin
相关产品推荐
相关产品推荐

