Telling SBT to use different JDK version

#1

Disclaimer: I already opened a similar question on Stackoverflow but I believe the number of Scala/sbt users is limited there.

Is there a way to tell SBT to use a different JDK than the one it detects automatically?

I am working on Windows 10 and have an installed JDK 11 for daily use. But I want to use JDK 8 (ZuluFX 8) to compile build my Scala application with sbt.

I was pointed to something called sbt-extras but to me, this looks like a bash script that won’t work on Windows.

Anyone here who could help me? Or should I just go for Gradle and ignore Scala’s own build system?

#2

SBT runs within the JVM, so usually you would use whatever the standard mechanism is to determine which Java you get when you run java, for instance the JAVA_HOME environment variable.

2 Likes
#3

Can you share more about your setup? How did you install SBT and how are you invoking it? Are you running it from the terminal? Using IntelliJ? Etc…

#4

I installed SBT with the MSI distribution on Windows 10 Professional and I want to use it as well on the command line and within IntelliJ.

IntelliJ does not seem to be a problem since I can there choose the JDK in the project structure.

If you have specific questions about my setup I am happy to anwer them.

#5

sbt -java-home <path/to/jdk> ... ? Seems to work with sbt 1.2.8.

#6

I tried: PS C:\Users\hannes> sbt.bat -java-home C:\opt\Zulu\zulufx-jdk8.0.212-win_x64\

But ended up with an error:

OpenJDK 64-Bit Server VM warning: Ignoring option MaxPermSize; support was removed in 8.0
OpenJDK 64-Bit Server VM warning: Ignoring option MaxPermSize; support was removed in 8.0
[info] Loading global plugins from C:\Users\hannes.sbt\1.0\plugins
[info] Loading project definition from C:\Users\hannes\project
[info] Set current project to hannes (in build file:/C:/Users/hannes/)
[error] Expected symbol
[error] Not a valid command: -
[error] Expected end of input.
[error] Expected ‘–’
[error] Expected ‘debug’
[error] Expected ‘info’
[error] Expected ‘warn’
[error] Expected ‘error’
[error] Expected ‘addPluginSbtFile’
[error] -java-home
[error] ^

#7

Then probably this option is only available with the Linux shell script and you’ll need to set the JAVA_HOME environment variable accordingly - perhaps in yet another batch file that calls sbt.bat.

#8

Do you mean: javacOptions ++= Seq("-source", “11”) ?

#9

It seems as if this was answered at StackOverflow, cross-posting the link for reference: https://stackoverflow.com/questions/56418191/how-to-set-up-sbt-to-select-a-specific-jdk-among-available-jdks

Note the JAVA_HOMES environment variable ending with S, not to be confused with JAVA_HOME.

#10

After all there is a bug in sbt.bat, I filed a bug report: https://github.com/sbt/sbt/issues/4768