Fork me on GitHub
#cider
<
2024-06-12
>
akis11:06:43

hey all, how do I set the JDK version?

> java -version

openjdk version "22.0.1" 2024-04-16
OpenJDK Runtime Environment Homebrew (build 22.0.1)
OpenJDK 64-Bit Server VM Homebrew (build 22.0.1, mixed mode, sharing)
However, when I start cider with:
[nREPL] Starting server via /opt/homebrew/bin/clojure -Sdeps \{\:deps\ \{nrepl/nrepl\ \{\:mvn/version\ \"1.0.0\"\}\ cider/cider-nrepl\ \{\:mvn/version\ \"0.45.0\"\}\ refactor-nrepl/refactor-nrepl\ \{\:mvn/version\ \"3.9.1\"\}\}\ \:aliases\ \{\:cider/nrepl\ \{\:main-opts\ \[\"-m\"\ \"nrepl.cmdline\"\ \"--middleware\"\ \"\[refactor-nrepl.middleware/wrap-refactor\,cider.nrepl/cider-middleware\]\"\]\}\}\} -M:cider/nrepl
I can see that it's using Java 11 in the REPL buffer
nREPL server started on port 51778 on host localhost - 
;; Connected to nREPL server - 
;; CIDER 1.14.0-snapshot (package: 1.14.0-snapshot), nREPL 1.0.0
;; Clojure 1.11.3, Java 11

1
oyakushev11:06:06

One option is to do C-u M-x cider-jack-in (that is, jack in with a universal argument). That allows you to edit the command line where you can put JAVA_HOME=/path/to/jdk in front, and that would set the JDK version.

oyakushev11:06:06

Otherwise, you have to somehow set JAVA_HOME env variable in Emacs, there are a few guides on the internet how to do that, but none of them worked for me reliably. What I do is that I set the env in the terminal shell and then launch Emacs from there so that Emacs inherits the java version I want.

akis11:06:26

I think JAVA_HOME is set correctly, running terminal directly from emacs:

❯ echo $JAVA_HOME
/opt/homebrew/opt/openjdk

❯ java -version
openjdk version "22.0.1" 2024-04-16
OpenJDK Runtime Environment Homebrew (build 22.0.1)
OpenJDK 64-Bit Server VM Homebrew (build 22.0.1, mixed mode, sharing)
This returns the same as running from outside of emacs, so I think emacs is correctly inheriting the environment. I've tried appending JAVA_HOME to the nrepl start command, and I get the same result - Java 11

akis11:06:33

oh, no, it does actually work

oyakushev11:06:45

/opt/homebrew/opt/openjdkWhat's inside that directory? It looks to me as the homebrew-installed Java does its own JDK selection under the hood.

👀 1
akis11:06:52

just had to put it in front of the whole command :man-facepalming:

oyakushev11:06:04

Yes, that's how env overriding works

akis11:06:43

yeah, stupid moment from my side 😅 though I still don't understand why the correct JAVA_HOME is not inherited... :thinking_face:

oyakushev11:06:42

I think it is correct, but there is one additional layer of indirection (that relies on I don't know what) that causes the thing in /opt/homebrew/opt/openjdk to select a different JDK

👍 1
oyakushev11:06:15

What does which java says inside Eshell? (vs terminal)

akis11:06:19

/usr/bin/java
in both cases

akis11:06:41

/usr/bin/java -version

openjdk version "22.0.1" 2024-04-16

oyakushev11:06:15

/usr/libexec/java_home -V

oyakushev11:06:49

In either Eshell or terminal

akis11:06:35

Matching Java Virtual Machines (4):
    21.0.1 (arm64) " Inc." - "Amazon Corretto 21" /Users/akis/Library/Java/JavaVirtualMachines/corretto-21.0.1/Contents/Home
    21.0.1 (arm64) "Oracle Corporation" - "OpenJDK 21.0.1" /Users/akis/Library/Java/JavaVirtualMachines/openjdk-21.0.1/Contents/Home
    17.0.4.1 (arm64) "Eclipse Adoptium" - "OpenJDK 17.0.4.1" /Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home
    1.8.0_382 (x86_64) "Eclipse Temurin" - "Eclipse Temurin 8" /Users/akis/Library/Java/JavaVirtualMachines/temurin-1.8.0_382/Contents/Home

akis11:06:50

(no difference between eshell/terminal)

oyakushev11:06:17

That's funny. This list has neither 22 nor 11 🙂

akis11:06:32

hah, true 😅 Seems like a start of a nice, deep, rabbit hole 😂 I can live with universal command though, that was really helpful! 🙇 I'm sure I messed up java installs long time ago on this machine, so probably that's what's causing the pain right now

👍 1
oyakushev11:06:43

Yeah, I'd start with picking one way of installing and managing JDKs, and then extend it to Emacs. Because right now it's hard to tell who's setting your JAVA_HOME and how it later translates to the exact JDK install path.

💯 1
oyakushev11:06:40

Some people have success with SDKMAN, I personally just download tarballs and extract them to /Library/Java/JavaVirtualMachines/. Then I have a shell function that allows me to quickly switch JAVA_HOME between them.

Harold16:06:27

I updated to the latest cider and I am getting this warning now when starting my repl:

;; Connected to nREPL server - 
;; CIDER 1.15.0 (Cogne), nREPL 1.2.0-beta2
;; Clojure 1.10.1, Java 11.0.22
;;     Docs: (doc function-name)
;;           (find-doc part-of-name)
;;   Source: (source function-name)
;;  Javadoc: (javadoc java-object-or-class)
;;     Exit: 
;;  Results: Stored in vars *1, *2, *3, an exception in *e;
;;  Startup: /home/harold/bin/lein update-in :dependencies conj \[nrepl/nrepl\ \"1.2.0-beta2\"\] -- update-in :plugins conj \[cider/cider-nrepl\ \"0.49.0\"\] -- repl :headless :host localhost
WARNING: CIDER requires cider-nrepl to be fully functional. Some features will not be available without it! (More information)
Probably some dependency version conflict in this project (the project has many dependencies). How can I start to track it down? Thanks! Also, I am not seeing prints in the repl - probably related to the above warning?

vemv16:06:37

I'd start by a ps aux | grep cider and see the cider version(s) present in your classpath

dpsutton17:06:58

(-> (System/getProperty "java.class.path") (clojure.string/split #":") (->> (filter #(clojure.string/includes? % "cider")))) from the repl should do it

Harold17:06:15

Both very helpful ideas! Indeed, it appears cider-nrepl is not on the classpath. :thinking_face:

Harold17:06:03

And it is on the classpath in the case of a new project (`lein new app foo`). So something with this project in particular.

vemv17:06:53

Lein update-in can be a bit wonky Do you have other plugins in that project?

Harold17:06:44

Ooooo, if I switch the new (foo) project to clojure 1.10.1 I start getting the warning.

Harold17:06:51

Did the new cider start requiring clojure 1.11?

vemv17:06:37

Nope https://docs.cider.mx/cider/about/compatibility.html#clojure Not immediately obvious to me why this would happen if you don't have Lein plugins around. @U06PNK4HG anything comes to mind?

Harold17:06:07

Cool, I'm happy to make an issue on the cider repo as well if that would be helpful.

Harold17:06:48

Yup - also confirmed: changing the clojure dep to 1.11.3 in the real project also gets rid of the warning (and print starts working again as well)

vemv17:06:33

> Cool, I'm happy to make an issue on the cider repo as well if that would be helpful. That would be welcome. Before that, you can try /home/harold/bin/lein update-in :dependencies conj \[nrepl/nrepl\ \"1.2.0-beta2\"\] -- update-in :plugins conj \[cider/cider-nrepl\ \"0.49.0\"\] -- repl :headless :host localhost (i.e. the same command emitted by CIDER) from the CLI. Does cider-nrepl disappear from the classpath too in that case? Also, are you on Lein latest? It has had more changes than usual in the last year or so I think.

Harold17:06:07

$ lein --version
Leiningen 2.10.0 on Java 11.0.22 OpenJDK 64-Bit Server VM

Harold17:06:58

Ah!

$ /home/harold/bin/lein update-in :dependencies conj \[nrepl/nrepl\ \"1.2.0-beta2\"\] -- update-in :plugins conj \[cider/cider-nrepl\ \"0.49.0\"\] -- repl :headless :host localhost
Warning: cider-nrepl requires Clojure 1.10.3 or greater.
Warning: cider-nrepl will not be included in your project.

vemv17:06:19

https://codeberg.org/leiningen/leiningen/releases/tag/2.11.2 is latest, probably it includes a more recent Clojure by default and solves the issue

Harold17:06:54

Yes, this project I'm working on explicitly pulls in 1.10.1

Harold17:06:19

Thanks so much for your help @U45T93RA6 and @U11BV7MTK, I learned a lot. 🙇

vemv17:06:19

Cheers. Normally I start cider-nrepl from the CLI as it is more debuggable. And it persists in case Emacs crashed/froze emacs-spin

Harold17:06:23

That's a neat idea, makes sense that there are some tradeoffs for the convenience of letting cider-jack-in do it.

oyakushev18:06:56

@U11BV7MTK @U0F7WES86 BTW, you can also press , in the REPL and select classpath.

😮 1
👍 1
dpsutton18:06:25

I forgot about the lovely menu behind comma. Good call out

👍 1
lassemaatta14:06:00

I just randomly bumped into this same issue when debugging why some old project (using clojure 1.10.0) had trouble running tests with cider

oyakushev14:06:00

This issue has been fixed in master, need a release of cider-nrepl now. https://github.com/clojure-emacs/cider-nrepl/pull/883

👍 2