requre deps in idea clojure repl, get FileNotFoundException - clojure

I created a new Leiningen project in idea, and imported some deps in project.clj as follows:
deps
and deps seems to be imported:
external libraries
But when I try to run repl, and required some deps that I imported in project.clj before, an FileNotFoundException has occurred:
FileNotFoundException
Starting nREPL server...
"E:\Program Files\Java\jdk-11.0.8\bin\java.exe" -Dfile.encoding=GBK -XX:-OmitStackTraceInFastThrow -Dclojure.compile.path=E:\idea_projects\clojure_test_second\target\classes -Dclojure_test_second.version=0.1.0-SNAPSHOT -Dclojure.debug=false "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2021.1.3\lib\idea_rt.jar=50688:D:\Program Files\JetBrains\IntelliJ IDEA 2021.1.3\bin" -classpath E:\idea_projects\clojure_test_second\test;E:\idea_projects\clojure_test_second\src;E:\idea_projects\clojure_test_second\dev-resources;E:\idea_projects\clojure_test_second\resources;E:\idea_projects\clojure_test_second\target\classes;C:\Users\asus\.m2\repository\org\clojure\clojure\1.10.1\clojure-1.10.1.jar;C:\Users\asus\.m2\repository\org\clojure\spec.alpha\0.2.176\spec.alpha-0.2.176.jar;C:\Users\asus\.m2\repository\org\clojure\core.specs.alpha\0.2.44\core.specs.alpha-0.2.44.jar;C:\Users\asus\.m2\repository\hiccup\hiccup\1.0.5\hiccup-1.0.5.jar;C:\Users\asus\.m2\repository\clojure\jdbc\clojure.jdbc\0.4.0\clojure.jdbc-0.4.0.jar;C:\Users\asus\.m2\repository\com\h2database\h2\1.4.193\h2-1.4.193.jar;C:\Users\asus\.m2\repository\nrepl\nrepl\0.6.0\nrepl-0.6.0.jar;C:\Users\asus\.m2\repository\clojure-complete\clojure-complete\0.2.5\clojure-complete-0.2.5.jar clojure.main -i C:\Users\asus\AppData\Local\Temp\form-init14732922726375964945.clj
Connecting to local nREPL server...
Clojure 1.10.1
nREPL server started on port 50817 on host 127.0.0.1 - nrepl://127.0.0.1:50817
(require '[clojure.java.jdbc :as jdbc])
Execution error (FileNotFoundException) at clojure-test-second.core/eval1555 (form-init14732922726375964945.clj:1).
Could not locate clojure/java/jdbc__init.class, clojure/java/jdbc.clj or clojure/java/jdbc.cljc on classpath.
jdk version is 11, idea version is 2021.1.3.
I searched solution for a long time, but didn't solve it. And I am a noob in clojure.
Thanks.

You have to
add [org.clojure/java.jdbc "0.7.12"] in project.clj file of your leiningen project folder. under :dependencies [ <add into this list> [org.clojure/java.jdbc "0.7.12"]].
Then you do $ lein deps from inside folder of your leiningen project folder, to ensure installation of that dependencies.
Then if you did M-x cider-jack-in from inside emacs opened inside project folder, you can do your require command. OR you just do $ lein repl from there and you can then do your (require '[clojure.java.jdbc :as j]) - voila!
Coming from Common Lisp, I was also quite lost like you.
Clojure requires Leiningen or Boot to function reasonably.
Whenever you have to install Clojure in a new machine, install first Leiningen, because you can install Cloure also using Leiningen.
With Boot, you can even create standalone scripts/executives which uses Clojure.
But also with Leiningen see here.
Install and use Boot
I think the way you want to use the repl - more freely -
suits more to Boot:
# install boot e.g. by (for linux)
$ sudo bash -c "cd /usr/local/bin && curl -fsSLo boot https://github.com/boot-clj/boot-bin/releases/download/latest/boot.sh && chmod 755 boot"
# upgrade!
$ boot -u
# create your project folders and move into toplevel
mkdir -p my-project/src
cd my-project
# then open repl
$ boot repl
;; within the repl:
;; declare resource-paths and dependencies
(set-env! :resource-paths #{"src"}
:dependencies '[[org.clojure/java.jdbc "0.7.12"]])
;; create a minimal project declaration
(task-options!
pom {:project 'my-project
:version "0.1.0"}
jar {:manifest {"Foo" "bar"}})
;; build the minimal project (to install the dependencies!)
(deftask build
"Build my project."
[]
(comp (pom) (jar) (install)))
;; call build
(build)
;; now, in the repl, you can do:
(require '[org.clojure/java.jdbc :as j])
Boot is more dynamic - since you can from the repl introduce dependencies and build.

Related

Cannot get REPL prompt with figwheel-main and Clojure Tools

My goal is to have a ClojureScript REPL (targeting Node.js) using Figwheel and Clojure Tools (not Leiningen). But when I launch Figwheel it seems to start correctly but never completes.
deps.edn
{:deps {org.clojure/clojure {:mvn/version "1.11.1"}
org.clojure/clojurescript {:mvn/version "1.11.60"}}
:aliases {:fig {:extra-deps {com.bhauman/figwheel-main {:mvn/version "0.2.18"}}}}
:paths ["src" "target" "resources"]}
dev.cljs.edn
{:main cljs-example.foo
:target :nodejs
:optimizations :none
:pretty-print true
:source-map true
:asset-path "js/dev"
:output-to "resources/public/js/dev.js"
:output-dir "resources/public/js/dev"}
Launch command
clj -M:fig -m figwheel.main -b dev -r
% clj -M:fig -m figwheel.main -b dev -r
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[Figwheel] Compiling build dev to "resources/public/js/dev.js"
[Figwheel] Successfully compiled build dev to "resources/public/js/dev.js" in 0.708 seconds.
[Figwheel] Watching paths: ("src") to compile build - dev
[Figwheel] Starting Server at http://localhost:9500
[Figwheel] Starting REPL
Prompt will show when REPL connects to evaluation environment (i.e. Node)
Figwheel Main Controls:
(figwheel.main/stop-builds id ...) ;; stops Figwheel autobuilder for ids
(figwheel.main/start-builds id ...) ;; starts autobuilder focused on ids
(figwheel.main/reset) ;; stops, cleans, reloads config, and starts autobuilder
(figwheel.main/build-once id ...) ;; builds source one time
(figwheel.main/clean id ...) ;; deletes compiled cljs target files
(figwheel.main/status) ;; displays current state of system
Figwheel REPL Controls:
(figwheel.repl/conns) ;; displays the current connections
(figwheel.repl/focus session-name) ;; choose which session name to focus on
In the cljs.user ns, controls can be called without ns ie. (conns) instead of (figwheel.repl/conns)
Docs: (doc function-name-here)
Exit: :cljs/quit
Results: Stored in vars *1, *2, *3, *e holds last exception object
Starting node ...
Node output being logged to: resources/public/js/dev/node.log
For a better development experience:
1. Open chrome://inspect/#devices ... (in Chrome)
2. Click "Open dedicated DevTools for Node"
And that's it! :(
It stays like that forever and I never get a prompt such as:
cljs.user=>
Addendum
I don't think my foo.cljs file has an error that would cause Figwheel to hang. When I launch a "native" REPL with:
clj -M -m cljs.main --target node --compile cljs-example.foo --repl
Then I get:
ClojureScript 1.11.60
cljs.user=> (require '[cljs-example.foo])
i am foo!!!!!!!!
nil
cljs.user=>

What is equivalent of leiningen :repl-options {:init-ns 'user} for tools.deps in Clojure?

I'm using Cursive and I set aliases to dev, when I run the REPL it does not load the namespace that defined in deps configuration file:
:aliases {:dev {:main-opts ["--init" "src/my/server/core.clj"
"--eval" "(my.server.core/-main)"]}}
I tried this at the command-line and it worked as expected, loading my.server.core and then running its -main function, so I suspect that Cursive is using -R on aliases rather than -A so it is only pulling in :extra-deps and not :main-opts (that's just a guess, I don't use Cursive). My best suggestion is to ask in the #cursive channel on the Clojurians Slack as that is the primary channel for Cursive support (as far as I know).
I'll also highlight Krisztian's comment that you could use "-m" "my.server.core" as your entire :main-opts since -m will load the namespace and run the -main within it.
However, those options are not the same as Leiningen's :init-ns -- what I think you need is:
{:aliases {:dev {:main-opts ["-e" "(require,'my.server.core)"
"-e" "(in-ns,'my.server.core)"]}}}
When you specify :main-opts, that will suppress starting a REPL:
$ clj -A:dev
#object[clojure.lang.Namespace 0x3dddbe65 "my.server.core"]
$
So you need to add -r to tell the Clojure CLI that you also want a REPL started:
clj -A:dev -r
#object[clojure.lang.Namespace 0x433ffad1 "my.server.core"]
my.server.core=> (doc -main)
-------------------------
my.server.core/-main
([& args])
I don't do a whole lot ... yet.
nil
my.server.core=>

Could not locate clojure/math/numeric_tower__init.class or clojure/math/numeric_tower.clj on classpath

On a fresh lein install, when I start the repl via lein repl and enter into the repl:
(use 'clojure.math.numeric-tower)
It throws an error:
FileNotFoundException Could not locate clojure/math/numeric_tower__init.class or clojure/math/numeric_tower.clj on classpath.
I'm new to Clojure so I don't really know how to fix this.
Lein version: Leiningen 2.7.1 (lein -v)
Clojure version: 1.8.0 ( (clojure-version) )
Add [org.clojure/math.numeric-tower "0.0.4"] to the project's dependencies (in project.clj) and restart the repl.

How can I run lein repl outside of a project?

I spent some time last night messing with my leinigen profiles.clj to get rid of all the errors that were being printed when starting cider in my project. Today I went to start a repl from the terminal (I like to keep one open while I work) but it didn't work. I thought it was a cider issue so I tried it from Emacs but even in Emacs if I'm not in a project the repl won't start.
Here's the error:
Error loading refactor-nrepl.middleware: clojure.lang.ArityException: Wrong number of args (4) passed to: StringReader, compiling:(abnf.clj:186:28)
Exception in thread "Thread-4" java.lang.RuntimeException: Unable to resolve var: refactor-nrepl.middleware/wrap-refactor in this context, compiling:(NO_SOURCE_PATH:0:0)
...
Caused by: java.lang.RuntimeException: Unable to resolve var: refactor-nrepl.middleware/wrap-refactor in this context
My ~/.lein/profiles.clj
{:user {:plugins [[lein-try "0.4.3"]
[refactor-nrepl "1.1.0"]
[cider/cider-nrepl "0.9.1"]]
:dependencies [[org.clojure/tools.nrepl "0.2.12"]
[acyclic/squiggly-clojure "0.1.4"]
^:replace [org.clojure/tools.nrepl "0.2.12"]
[refactor-nrepl "1.1.0"]]}}
The versions of things when cider starts in a project
; CIDER 0.9.1 (Java 1.8.0_45, Clojure 1.7.0, nREPL 0.2.12)
I'm still pretty new to Clojure, Leinigen, Emacs, etc so I'm not sure why everything above made made my cider errors go away but it did. The cider errors I was getting were having to do with the nrepl version being too low and not having certain things installed (like refactor-nrepl).
When starting a repl from lein using lein repl, it really wants to run in a lein project dir. I keep an empty lein project named clj around in my home dir for this purpose. That way, my common dependencies are already there in the project.clj file, and lein is pre-configured just the way I like it.
You can start lein repl in an empty dir, but you get 10-20 error messages each time before it starts.
Another way is to use the plain repl built into the clojure jar file:
~/dummy > cp /home/alan/.m2/repository/org/clojure/clojure/1.8.0-RC1/clojure-1.8.0-RC1.jar .
~/dummy > d *
-rw-rw-r-- 1 alan alan 3935726 Nov 19 14:11 clojure-1.8.0-RC1.jar
~/dummy > java -jar clojure-1.8.0-RC1.jar
Clojure 1.8.0-RC1
user=>
As you can see, I created an empty directory named dummy and copied in the clojure-*.jar file. You can then run it with the syntax java -jar xxx.jar and it will fire up a repl completely independently of lein.
I also just keep a scratch project which I use for quick/simple repl sessions. There is a lien-oneoff plugin which is supposed to make it easy to work with simple single file lein projects which might be useful.
The other thing you could do is setup a boot config for basically getting a repl up to work with
what is your lein version, I am use 2.5.3, I can start lein repl anywhere.
Shell:~ >: lein repl
nREPL server started on port 52343 on host 127.0.0.1 - nrepl://127.0.0.1:52343
REPL-y 0.3.7, nREPL 0.2.10
Clojure 1.7.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_60-b27
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
user=> Bye for now!
Shell:~ >: lein version
Leiningen 2.5.3 on Java 1.8.0_60 Java HotSpot(TM) 64-Bit Server VM
Shell:~ >: cat .lein/profiles.clj
{:1.2 {:dependencies [[org.clojure/clojure "1.2.0"]]}
:1.3 {:dependencies [[org.clojure/clojure "1.3.0"]]}
:1.4 {:dependencies [[org.clojure/clojure "1.4.0"]]}
:user {:plugins [[lein-immutant "2.0.0-alpha2"]
[lein-clojars "0.9.1"]
[lein-ancient "0.5.5"]
[lein-kibit "0.0.8"]
[lein-try "0.4.3"]
[venantius/ultra "0.2.0"]]
:ultra {:color-scheme :solarized_dark}}}

How can new clojure libraries be loaded in the repl

So I have this workflow problem:
I'm happily typing away on my clojure project repl and realise that i need another library that is not in my project.clj, say in this case, i needed the tools.cli library.
I open up project.clj in my editor and add in an entry to the :dependencies
[org.clojure/tools.cli "0.2.1"]
Then, within the project directory, I type lein deps in the shell to pull in the necessary libraries
After the project dependencies are pulled, technically all the class files are already there ready to be loaded, but if I go back to my repl and type:
> (use 'tools.cli)
I get this:
=>> FileNotFoundException Could not locate tools/cli__init.class
or tools/cli.clj on classpath: clojure.lang.RT.load (RT.java:432)
So I would have to restart my repl, wasting a whole heap of time reconfiguring the state of the repl to where I was before I needed the library.
Is there a way to just load in the library dynamically? eg, after I run lein deps I just go back to the repl and type:
> (load-library "tools.cli")
> (use 'tools.cli)
Thanks in advance
Pomegranate is for you:
https://github.com/cemerick/pomegranate
It supports download and addition of new dependencies at runtime, e.g.:
(add-dependencies :coordinates '[[incanter "1.2.3"]]
:repositories (merge cemerick.pomegranate.aether/maven-central
{"clojars" "http://clojars.org/repo"}))
Will something like this work for you?
https://groups.google.com/d/msg/clojure/AJXqbpGMQw4/0-7-3pXRwGkJ
There is also clojure.core/add-classpath, but it's deprecated.
http://clojuredocs.org/clojure_core/clojure.core/add-classpath
You can try out one library in the repl using lein-try.
~/.lein/profiles.clj:
{:user {:plugins [[lein-try "0.4.3"]]}}
command line:
$ lein try clj-time "0.5.1"
Fetching dependencies... (takes a while the first time)
lein-try loaded [clj-time "0.5.1"]
nREPL server started on port 57036
REPL-y 0.2.0
Clojure 1.5.1
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
user=>