Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Java version for SBT

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.

like image 741
LondonMassive Avatar asked Apr 20 '26 13:04

LondonMassive


2 Answers

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")
like image 192
samthebest Avatar answered Apr 23 '26 03:04

samthebest


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

like image 39
Vimit Dhawan Avatar answered Apr 23 '26 04:04

Vimit Dhawan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!