How can I run lein repl outside of a project? - clojure

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}}}

Related

requre deps in idea clojure repl, get FileNotFoundException

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.

Cider version does not match cider-nrepl version in new luminus project

I'm experimenting with luminus, and all new luminus projects are giving me the cider version/ cider-nrepl version mismatch error when I connect to the repl started by lein run using emacs M-x cider-connect:
WARNING: CIDER's version (0.14.0) does not match cider-nrepl's version (nil). Things will break!
If I ignore the automatically started nrepl and use M-x cider-jack-in to start a new repl I don't see the error. This is what I would normally use for a clojure project but there appear to be certain things that don't work as well using luminus (starting and stopping an h2 database if I recall correctly, but that is another issue).
I have removed my ~/.lein/profiles.clj file and replaced it with one containing just the cider-nrepl plugin, ie:
{:user
{:plugins
[[cider/cider-nrepl "0.14.0"]]}}
I have also tried adding the plugin via the project.clj file but I still get the error.
lein deps :tree gives me a few possible conflicts and suggests exclusions, but none of them involve nrepl or cider.
What am I missing here?
I seems you are not using ciders nrepl but luminus-nrepl - therefor you get
not match cider-nrepl's version (nil)
if you create a luminus project like so
lein new luminus <project-name> +cider
the warning should disappear.
addendum, lein deps :tree (which was a good approach to analyse the problem)
without +cider
[luminus-nrepl "0.1.4"]
[org.clojure/tools.nrepl "0.2.12" :exclusions [[org.clojure/clojure]]]
with +cider
[cider/cider-nrepl "0.15.0-20170626.002218-19"]
[luminus-nrepl "0.1.4"]
[org.clojure/tools.nrepl "0.2.12" :exclusions [[org.clojure/clojure]]]

Clojure ring server seems to be missing dependencies on slf4j

I've been trying to follow a number of tutorials on building a web app in Clojure, but I keep running into the same problem. To take the simplest case, I tried following this tutorial: http://drtom.ch/posts/2012-12-10/An_Introduction_to_Webprogramming_in_Clojure_-_Ring_and_Middleware/
When I get to the step that starts the server (run-jetty handler {:port 8383}), I get the following error:
NoSuchMethodError org.slf4j.helpers.MessageFormatter.arrayFormat(Ljava/lang/String;[Ljava/lang/Object;)Lorg/slf4j/helpers/FormattingTuple; org.eclipse.jetty.util.log.JettyAwareLogger.log (JettyAwareLogger.java:613)
I asked lien to show me the classpath, and sure enough, org.slf4j.helpers.MessageFormatter isn't in there anywhere.
I've run into this on pretty much every ring-based web tutorial I've tried, so either I've got something configured weird (I updated and reinstalled lein, blew away my ~/.m2 and rebuilt, etc), or something has changed in the myriad dependencies that get put together to make the classpath.
Any ideas what's going on here?
EDIT
I've got further information -- I created a VM in virtualbox, installed OpenJDK and lein, and created a project there. It worked fine. Since I had created it in a directory shared with the host, I then tried doing "lein ring server" in the same directory from the host, and it failed with the above error.
So I did "lein classpath" both in the vm and in the host and compared the results -- they're identical. I also checked that they're running the same build of the same JVM (OpenJDK 64-bit build 24.51-b03).
So, if they're running the same JVM with identical classpaths, what's left?
Can you try updating the dependencies like the following?
(defproject ..........
:dependencies [[org.clojure/clojure "1.5.1"]
[ring/ring-core "1.1.8"]
[ring/ring-jetty-adapter "1.1.8"]
[compojure "1.1.3"]]
:main quickstart.core
:min-lein-version "2.0.0"
:plugins [[lein-ring "0.8.10"]]
:ring {:handler quickstart.core/handler})
If you use the lein ring plugin as configured above, you can start the application like:
lein ring server

Leiningen fails to start a REPL on WinXP machine

I'm using a WinXP (sp3) machine with clojure-1.5.1, leiningen-2.3.4, and the Java 1.7.0_45 jdk. I have no trouble creating a new project with leiningen, starting a clojure shell, or creating and running java programs. However, the following command (please see below) results in the error message(s) displayed. I have added the clojure-1.5.1 and leiningen-2.3.4-standalone.jars to user and system path variables in addition to the creation of a new user and system environmental variable, LEIN_JAR that points to the location of the leiningen.jar file. The classpath for both the user and system variables has also been edited as above. My lein.bat is not missing any double quotes around the location of the LEIN_JAR setting as another poster stated in assisting another beginning(?) user of Clojure. Any constructive help would be appreciated.
My project.clj file :
(defproject test10 "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]])
Edit 2: lein version command output
C:\>lein version
Leiningen 2.3.4 on Java 1.7.0_45 Java HotSpot(TM) Client VM
C:\>lein repl
Error: Could not find or load main class -Dfile.encoding=UTF-8
Exception in thread "Thread-4" clojure.lang.ExceptionInfo: Subprocess failed {:e
xit-code 1}
at clojure.core$ex_info.invoke(core.clj:4327)
at leiningen.core.eval$fn__3532.invoke(eval.clj:226)
at clojure.lang.MultiFn.invoke(MultiFn.java:231)
at leiningen.core.eval$eval_in_project.invoke(eval.clj:326)
at clojure.lang.AFn.applyToHelper(AFn.java:167)
at clojure.lang.AFn.applyTo(AFn.java:151)
at clojure.core$apply.invoke(core.clj:619)
at leiningen.repl$server$fn__7443.invoke(repl.clj:201)
at clojure.lang.AFn.applyToHelper(AFn.java:159)
at clojure.lang.AFn.applyTo(AFn.java:151)
at clojure.core$apply.invoke(core.clj:617)
at clojure.core$with_bindings_STAR_.doInvoke(core.clj:1788)
at clojure.lang.RestFn.invoke(RestFn.java:425)
at clojure.lang.AFn.applyToHelper(AFn.java:163)
at clojure.lang.RestFn.applyTo(RestFn.java:132)
at clojure.core$apply.invoke(core.clj:621)
at clojure.core$bound_fn_STAR_$fn__4102.doInvoke(core.clj:1810)
at clojure.lang.RestFn.invoke(RestFn.java:397)
at clojure.lang.AFn.run(AFn.java:24)
at java.lang.Thread.run(Thread.java:744)
REPL server launch timed out.
C:\>
Thank you for the response. The output from lein version looks good. Sorry for the long message :)
Here is my approach to trouble shooting this further. Please make sure you are checking the system logs maintained by Windows. Pertinent messages could be routed to the system logs and not displayed in the console.
So far it appears leinegen can execute, some, but not all of it's commands. If you execute "lein help", you will get list of available commands.
My first thought is, leinegen is having trouble with the "lein repl" defaults. Thus, instead having leinegen choose default values for host and port, set them explictily in the command line. For example: "lein repl :start :host localhost :port 5554" or "lein repl :start :host 127.0.0.1 :port 5554".
I would change in to your "example" directory and verify that "lein deps", "lein check" and "lein compile" all run error free.
Finally, I would edit the example project. Add the following line to project.clj (right after the :dependencies clause)
:main example.core
That line tells leinegen what the entry point of the "example" program is.
Next we need to edit the "src/example/core.clj" file. Delete the contents and replace it with the following "hello word":
(ns example.core)
(defn -main [& args ]
(println "Hello, World!"))
Now, from the root of the example project (C:/>example) run "lein run". Please post the results. If "lein run" succeeds try "lein repl" one more time from the root of the example project.
Again. make sure you check the Window's logs for messages. It might also help to run "lein deps", "lein check" and "lein complile" from the root of the example project. See if any errors are raised that are more helpful.
Sorry I don't use MS Windows. However I have a couple suggestions.
You said you were able to create a new project with leiningen. What disk/directory did you create that project in?
Per your post, it looks like you are issuing the "lein repl" command from the root disk directory of your "c:" drive. Is that where you created a project?
If you change to a different disk/directory, do you get the same error message?
Perhaps leiningen is picking some artifacts it can't resolve in your root directory (I.E. C:/>). Thus, check to see if you can run "lein repl" from a different disk directory.
If you are only getting an error when you running "lein repl" from the root of a project directory, please post some information about the project (E.G. the contents of your project.clj file).
Hope that helps.
Check to make sure that lein is not in a directory whose path has any spaces or capitalized characters in it.
Make sure the JDK is in a directory with no spaces or capitalized characters in it.
Make sure JAVA_HOME is set
Make sure java is in your SYSTEM path

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=>