How to set Java version for SBT
Asked Answered
S

4

8

I am trying to run a scala program, in which there are errors with Java 16. My colleague is using Java 15, and all is fine. When i type java -version in my terminal it says i am using Java 15. However, when i run sbt run -v, it says it is using Java 16, and thus the program throws errors.

I am seeing people talk about this sbt-extra thing, but not a whole lot of explanation on how to use it. I do not even have Java 16 installed on my Mac, so I am really confused as to why SBT says this.

Suspicion answered 1/7, 2021 at 12:6 Comment(3)
sbt should just uses what is on the JAVA_HOME If you are sure you do not have Java 16 installed then it shouldn't use that, sbt doesn't download JDKs.Anderer
If it is using Java 16 then its definitely installed somewhere. Check your java installation folder which can be found by following which java. You can set JAVA_HOME or use sbt -java-home <path/to/jdk> to use specific jdk.Winery
SET JAVA_HOME=C:/Program Files/Java/jdk-16.0.1 <next line> sbtLenardlenci
G
6

Add this to ~/.bash_profile, ~/.bashrc or ~/.zshrc (depending on shell preferences/OS), or just run this before running sbt in a terminal:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home

It's better though to do this per-sbt project via build.sbt as per this answer: https://mcmap.net/q/1285138/-how-to-set-java-version-for-sbt

Grandam answered 1/7, 2021 at 13:26 Comment(0)
V
5

The best way is to do this via the build.sbt file so then it's specific to just that project not your entire local environment. I.e. add the following to build.sbt:

  javacOptions ++= Seq("-source", "11", "-target", "11")
Voluble answered 12/6, 2023 at 11:39 Comment(2)
if both javacOptions and JAVA_HOME are set, sbt will use JAVA_HOMESheri
-source X will not help if a method becomes removed in your current VM (but was present in jdk X).Belldame
R
2

To handle your installed jvms you can use Jenv.

To install jenv:

git clone https://github.com/jenv/jenv.git ~/.jenv
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile

echo 'eval "$(jenv init -)"' >> ~/.bash_profile

Then, you can add your intalled jvms. In Mac, if you have installed them via brew you can find those in: /Library/Java/JavaVirtualMachines.

Then add them to jenv:

jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home 
jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home 

You can see the available jvms in jenv:

jenv_versions

you can set the default jvm with the command:

jenv global 1.8.0.121

Then, execute sbt in some of your projects and you should see that jvm as the jvm that sbt is using.

Rubio answered 1/7, 2021 at 13:28 Comment(2)
I did not know about this thing, Its great toolGrandam
I'd like to use jabba. I think that is very similar to jenv at this point..Gerik
E
1

Another option which worked for me is to add the version of java you want to be used to the front of your terminal PATH environment variable. Since I used homebrew to install openjdk, I used the path they suggested resulting in the following path to use openjdk version 11.

export PATH="usr/local/opt/openjdk@11/bin:$PATH"

Note - the openjdk path I used I think is just the homebrew symlink to the actual java installation which is in /Library/Java/JavaVirtualMachines. You could probably just use that actual path but I didn't test it.

Edieedification answered 2/6, 2022 at 14:48 Comment(1)
I do believe that setting the JAVA_HOME variable is a better option. I just wanted to give this option in case other things don't work for someone.Edieedification

© 2022 - 2024 — McMap. All rights reserved.