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.
Related
I have just started learning clojure, but have a hard time understanding why my file structure is erroneous. In the main file(main.clj),
I have just this:
(ns example.core
(:gen-class)
(:load "declare"))
...some code...
and in 'declare.clj', which is in the exact same classpath "project/src", I have this:
(in-ns 'example.core)
...some code...'
From what I understand I should be hitting the right syntax, but I only receive
Could not locate
clojurepractice2/src/clojurepractice2/declarations__init.class or
clojurepractice2/src/clojurepractice2/declarations.clj on classpath.
from REPL. I am using lein to code, which I know is supposed to automatically set the classpath. Is there something I am missing?
I have tried using load-file With the same results.
The main.clj should have namespace example.main. Or rename it to core.clj. This is definitely an issue with file name and namespace mismatch.
Because the file name should be same as the namespace name.
If your file is in src/example/main.clj then the namespace should be example.main.
The usage is
(ns example.main
(:gen-class)
(:load "declare"))
Is the file path as src/example/main.clj ? Is the main inside the example folder in src?
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'm new with clojure and the jvm and am having trouble creating a leinengen project where I can separate my code into namespaces. My project is named cloj_test and in my cloj_test/src/cloj_test directory I have a file named db_connect.clj. In the db_connect.clj file I have code at the top to define the namespace
(ns db-connect
(:require [clojure.java.jdbc :as jdbc]
[clojure.java.jdbc.sql :as sql]))
and then a few functions to define some database functionality. cloj_test/src is on the lein classpath. When I launch the repl using "lein repl" and then type
(require 'cloj-test.db-connect)
It works. However, if I type
(require '[cloj-test.db-connect :as db])
I get this error:
Exception namespace 'cloj-test.db-connect' not found clojure.core/load-lib (core.clj:5380)
The same thing happens when I use "use". Does anybody know how I can resolve this?
The package name is missing form the ns declaration
(ns cloj-test.db-connect
(:require [clojure.java.jdbc :as jdbc]
[clojure.java.jdbc.sql :as sql]))
The namespace declaration needs to match the directory that the file is in
(except that -s are changed to _s) in the file and directory names.
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."