{:deps
com.google.code.gson/gson {:mvn/version "2.9.0"}}
}
in core.clj: evaluate
(ns core
(:require [google.code.gson/gson :refer :all]
)
(:gen-class))
I get exception:
Unhandled clojure.lang.Compiler$CompilerException
Call to clojure.core/ns did not conform to spec.
I just want to load gson into my namespace. What am i doing wrong?
Group/Artifact coordinates (what you are using to resolve dependencies;
e.g. com.google.code.gson/gson) are not related to what is in
those artifacts. So you can not just require the artifact. You have to
require the namespaces of what is in them or more likely in this case
import the classes that are in them (it's a java library more likely
and not a Clojure library).
The error you get there is due to the / in the require. And even if
you the needed . instead you will see an error, that no such namespace
exists.
(ns core
(:gen-class))
(import [com.google.gson Gson])
seems to work.
But why do not need the code part in com.google.code.gson?
Related
I have create a project with two files core.clj and module.clj.
On core.clj to be able to use function of module.clj I have declared :
(ns my-project.core
(:require [my-project.module :as mod]))
And on module.clj to be able to use function of core.clj I have declared :
(ns my-project.module
(:require [my-project.core :as core]))
Now, when I use 'cider-load-file on core.clj, I have an error due to function of module.clj, and if i start with module.clj, I have a error due to function of core.clj.
Is their a way to fix the issue, or is my code should be refactored ? It seems that I am able to build the jar with lein.
You must refactor to avoid dependency cycles. This is a feature of Clojure, not a requirement added by Cider or waived by Leiningen. "Building a jar" of Clojure stuff just means zipping it up, and thus is not a sign that the stuff is error-free.
This kills me... I cannot require anything other than built-in native deps. No hiccup, no http-kit etc... Even if I can find them on my hard drive in .m2/repository
lein new myapp,
add [markdown-clj 0.9.91] to project.clj,
add (ns metapp.core
(:require [markdown-clj :as mark]) )
lein run
Retrieving markdown-clj/markdown-clj/0.9.91/markdown-clj-0.9.91.pom from clojars
Retrieving markdown-clj/markdown-clj/0.9.91/markdown-clj-0.9.91.jar from clojars
Exception in thread "main" java.io.FileNotFoundException: Could not locate quote/markdown_clj__init.class or quote/markdown_clj.clj on classpath. Please check that namespaces with dashes use underscores in the Clojure file name.,
lein deps is not returning anything
Somebody knows what's wrong please? This stacktrace isn't very helpful, so Lein can fetch deps, but doesn't know how to require them?
EDIT: Running Linux Mint 18.0, clojure is in /home/denis/clojure-1.8.0 and is called by alias java -cp /home/denis/clojure-1.8.0/clojure-1.8.0.jar clojure.main. Directory tree in myapp is /home/denis/prg/cljr/myapp
SOLUTION:
Thank you guyz, but now feel like an idiot.
So to summarize for future-comers, in project.clj, to ask for a dependency "X" doesn't mean you should "require" it as "X". You must require it the way the author specified in documentation, for example [http-kit "2.2.0"] in project.clj is required as follows
(ns metapp.core (:require [org.httpkit.client :as http] ).
Second, the way you require inside your code is not the same as the way you require in REPL, for example, this works in yourapp.core (require [stuff.core as stuff]). You could also write it like this, it works too (ns yourns (:require [stuff.core :as stuff]). But this synthaxe doesnt work: (:require [stuff.core :as stuff]).
In REPL however, it's different story! I must use (:require '[stuff.core]) if it is an added dependency, or (:require 'clojure.string) if it is a built-in library! Note that something like (require '[http.async.core]) doesn't work because it is not built-in. So if you checked the documentation https://clojuredocs.org/clojure.core/require which only shows built-in examples, like me, you are doomed.
Also for built-in library like clojure.string you can use simply (require 'clojure.string), yup, the one which didn't worked with dependencies. Have fun guys! LOOOOONNG journey ahead, clojure is only language so far I needed to spend 4 days figuring out how to IMPORT modules (poke Python, it only took 30 seconds), hope it worth it!
You should require markdown.core. From the documentation for that project:
(ns foo
(:use markdown.core))
In your case:
(ns metapp.core
(:require [markdown.core :as mark]))
should work.
Not realising that the name of the library and the namespaces that make up the library are different things is something that is easy to be tripped up by.
markdown-clj is just a name of a package. But when you require something, you need to specify a module, not a package. Most of the packages have core module so the proper usage would be:
(:require [markdown-clj.core :as mark])
In a fresh lein (~2.5) repl, I type:
(require '[clojure.string :as string])
And I can use string as expected. However, when trying to require core.asnc like so, I get an error msg:
(require '[clojure.core.async :as ca])
FileNotFoundException Could not locate clojure/core/async__init.class or clojure/core/async.clj on classpath. clojure.lang.RT.load (RT.java:449)
thanks to your answers I managed now to require arbitrary libs in a repl on runtime using pomegranate or alembic. But what about macros? How - for example - do I get the 'go' macro in the repl? There isn't something like (require-macros ... in analogy to the approach one would take when requiring core.async in a project's ns declaration.
core.async is not part of the clojure.core library. You need to add the core.async jar or sources to your classpath. The easiest way to do this is with a dependency via project.clj, but there are also tools like pomegranate and alembic for doing this at runtime, which could be added to your local profiles.clj.
I am using the following namespace for a graphics demo
(ns foo.core
(:use [clojure.browser.dom :only [get-element]]))
However, I return a File not found exception for clojure browser dom in the classpath.
Clojurescript has been pulled, and is contained within the file I cd into. But is not contained in the file I am trying to load, after having accessed the REPL.
Is clojure.browser.dom out of date? Or, am I missing something within the implementation?
Edit
I have not included the dependency for this file.
That namespace is correct:
https://github.com/clojure/clojurescript/blob/master/src/cljs/clojure/browser/dom.cljs
Maybe the problem is the ns form? Try
(ns foo.core
(:require [clojure.browser.dom :refer [get-element]]))
Also, just in case, be sure to name your file .cljs and restart the cljsbuild compiler, just in case.
The error as shown on the Noir error page: java.io.FileNotFoundException: Could not locate boundaries/lat_long__init.class or boundaries/lat_long.clj on class path
The code that requires it:
(ns boundaries.views.boundary
(:use noir.core
hiccup.core
hiccup.page-helpers)
(:require
[boundaries.lat-long :as lat-long]
[noir.response :as resp]))
Why is it looking for lat_long instead of the specified lat-long? boundaries/lat-long.clj exists as well as the corresponding boundaries.lat-long namespace.
the JVM does not allow -s in class names so the Clojure compiler converts them to _s
the problem is most likely with the project.clj dependencies.
When diagnosing this sort of issue:
is the namespace available from the REPL?
does the .class file appear in the lib directory for the project?
re-run lein deps
You need to rename the boundaries/lat-long.clj to boundaries/lat_long.clj.
Note that you don't have to change the namespace name. The clojure convention is to use "-" for functions and namespace names.
From Stuart Sierra response at https://stackoverflow.com/a/4451693/151650: "It's a necessary workaround for Java interoperability."