Dependency not working

Hi,
This is my first question here. I asked the question on SO, got no answer.
So I started a project with lein new app. I added a dependency in the project.clj ([org.clojure/data.json "2.0.1"])
In core.clj I require the library as follows: (:require [clojure.data.json :as json])

when I try to use the library in: (def posts (json/read-str (slurp "../data.json") :key-fn keyword))
it throws this error: CompilerException java.lang.RuntimeException: No such namespace: json, compiling:

please let me know what step I am missing here. please explain the process of running the whole thing as if you’re talking to a 5 year-old!!!

Thanks!

All of the steps you describe sounds good, but they depend a little bit on some of the context around them. If you are willing to publish your complete code and directory structure in a small Github repository, for example, and whether you are using Leiningen or the Clojure CLI tools (or something else), that might help others help you spot the problem more quickly.

1 Like

Hey @eternalNewbie!

I tried running your program and it works for me (I used an example json, and changed the path).

Most likely you haven’t evaluated your ns function

(ns example-namespace
  (:require [clojure.data.json :as json]))

in your REPL and when you are trying to evaluate your next line you get the logical exception that it is not known what json is.

If you look at the bold text it means that the referred namespace is unknown for the REPL.

You can troubleshoot a message about “no such namespace” by inspecting the java.class.path system property:

(System/getProperty "java.class.path")

Hey @duzunov I am using Atom and I tried to evaluate the ns function and I get this error: FileNotFoundException Could not locate clojure/data/json__init.class or clojure/data/json.clj on classpath

Could do you tell me how you evaluate the ns function? (what editor you’re using?)
Thanks

Hi! I’m using Emacs+CIDER. I don’t know what is your editor setup, however as a principle before you evaluate commands you need to start a REPL (which includes getting any dependencies) and then to connect/“jack-in” to it.

I can suggest to try the following first - run lein repl first, and if you don’t have any errors type in your program there, form by form.

Hi! I’m trying it in Emacs-Cider as well. Probably there’s something wrong in my code then.
This is my project.clj:

            :dependencies [[org.clojure/clojure "1.10.0"]
                                     [org.clojure/data.json "2.0.1"]]
             :main ^:skip-aot myapp.core
             :target-path "target/%s"
             :profiles {:uberjar {:aot :all}})

and this i core.clj:

(ns myapp.core
  (:require [clojure.data.json :as json])
  (:gen-class))

(def posts (json/read-str (slurp "../../data.json") :key-fn keyword))

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (println "Hello, World!"))

Hmm looks like you pasted just part of the project.clj

Did you try to run lein repl or jack-in in cider (C-c M-j)?

here is the complete project.clj. Sorry for the formatting:
(defproject myapp “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 “Eclipse Public License 2.0 | The Eclipse Foundation”}
:dependencies [[org.clojure/clojure “1.10.0”]
[org.clojure/data.json “2.0.1”]]
:main ^:skip-aot myapp.core
:target-path “target/%s”
:profiles {:uberjar {:aot :all}})

I tried cider-jack-in. getting the same error!

I think your code is ok as I can run it. Do a lein clean and then a lein repl

@duzunov just to make sure I understand you, you run these commands in the command line, right? at the root of the project or at src?
Thanks again!

Yes, you run them at the project root. I assume you have project.clj at the root and your source at src/myapp/core.clj

Yes that’s what I did. I’ll give it a try again

Adding a dep in project.clj or deps.edn doesn’t automatically add it to your running JVM. So you need to restart your REPL or look into the add-libs branch of tools.deps, or for Lein projects I think it’s pomegranate?

Hi! it’s working now. Here’s what I did: I used Emcas-Cider. I opened a repl. I loaded the buffer in the repl. then from the repl, I called the function that calls the json library. And it works.
So I guess maybe I’m missing some plugin /config in Atom. Anyway, I will stick to Emacs for the moment till I figure out what’s going on with my Atom config.

thank you anyway for your help, you definitely guided me into the right solution!

Cheers!

1 Like

Hi, I also use Atom. What plug-ins are you using in Atom?

(just for the record, I’m only asking so that it can become easier to debug the problem)

Hi @mauricioszabo No problem. I have installed language-Clojure, proto-repl, Slima, Parinfer. Hope this helps!

Right, thanks. Have you tried Chlorine? I believe proto-repl is abandoned (also, I’m the author of Chlorine, so maybe I can help you better with it).

Even then, it’s strange - seems like a dependency problem that is not dependent of the current editor plug-in you’re using…