4

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.

LondonMassive
  • 367
  • 2
  • 5
  • 20
  • **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**. – Luis Miguel Mejía Suárez Jul 01 '21 at 12:32
  • 3
    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 ` to use specific jdk. – sarveshseri Jul 01 '21 at 12:34
  • SET JAVA_HOME=C:/Program Files/Java/jdk-16.0.1 sbt – Mikhail Ionkin Dec 04 '22 at 10:46

4 Answers4

3

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://stackoverflow.com/a/76456295/1586965

samthebest
  • 30,803
  • 25
  • 102
  • 142
Vimit Dhawan
  • 597
  • 1
  • 7
  • 25
1

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.

Emiliano Martinez
  • 4,073
  • 2
  • 9
  • 19
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.

donlja
  • 11
  • 3
  • 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. – donlja Jun 02 '22 at 16:51
0

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")
samthebest
  • 30,803
  • 25
  • 102
  • 142