Telling SBT to use different JDK version

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?

1 Like

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.


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…

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.

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

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] ^

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.

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

1 Like

It seems as if this was answered at StackOverflow, cross-posting the link for reference:

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

After all there is a bug in sbt.bat, I filed a bug report:

You can try with below command

sbt -J-java-home=/usr/java/jdk1.7.0_21/ -v
(my default jvm is 8, but above starting sbt explicitly with jdk-7 version)

An easier option is using sdkman, this way you can manage and automate per project intended Java usage. Open terminal at your project root directory and issue sdk env init then open created .sdkmanrc file with text editor of your choice and specify a (previously installed by sdkman) Java then save the file. Any time in future before you run sbt, just type sdk env to dictate intended Java.

javacOptions ++= Seq("-source", "11", "-target", "11")

solved it in my case

FTR, sbt.bat supports the -java-home option since version 1.5.1 (make sure to have at least this version of the launcher, the sbt.version setting in your project’s file is not relevant)