Is there a way to tell SBT which (locally) installed JDK to use?
I am quite certain I could change PATH
and JAVA_HOME
but I'd rather not change those settings since they apply for the whole system (Windows in that case).
I am more looking for a command line parameter of some sort.
For sbt users, JDK 11 support requires minimum sbt version 1.1. 0. sbt 1.3. 9 or newer is recommended.
Run the SBT console and then type sbtVersion to check the SBT version, and scalaVersion for the Scala runtime version.
If you're looking to specify a JDK for running SBT (rather than a JDK to use for running your code and/or tests from within SBT), you can make use of the JAVA_HOMES
environment variable, plus a .java-version
file in your project.
This is described in SBT's sbt.bat
file (typically installed to C:\Program Files (x86)\sbt\bin
) as a "poor man's jenv
", which isn't currently available on Windows.
(If you're looking for a similar solution for Linux or MacOS, you can either use jEnv
, or specify the Java home directory via the -java-home
SBT command line option—which also, sadly, isn't currently implemented on Windows.)
The JAVA_HOMES
environment variable (not to be confused with JAVA_HOME
) is used by SBT to identify a directory that contains one or more JDK installations. For example, if you're using AdoptOpenJDK's JDK distributions (recommended on Windows, if Oracle's new licensing restrictions are a problem for you), then this would typically be defined as C:\Program Files\AdoptOpenJDK
.
Let's say that you have two such JDK installations in the JAVA_HOMES
directory: one in a subdirectory named jdk-8.0.212.03-hotspot
; another in the jdk-11.0.3.7-hotspot
subdirectory. You can select which JDK you want to use, on a project-by-project basis, by creating a file called .java-version
in the root directory of each SBT project. To use the JDK in the jdk-8.0.212.03-hotspot
subdirectory, this file should then contain:
jdk-8.0.212.03-hotspot
When you run the sbt
command, if you have JAVA_HOMES
defined, SBT will look for a .java-version
file in the project's root directory. If it finds it, it creates a local version of JAVA_HOME
that is defined as JAVA_HOMES
plus the last line of .java-version
. It also adds this JAVA_HOME
's bin
directory to the path. (It also creates a JDK_HOME
process-local environment variable with the same value.)
This should do what you want. Although it's not a command line-based solution, it doesn't adversely affect other users, and allows each SBT project to be configured individually. If you do not have permission to create a system-wide environment variable, you should still be able to create a user-specific JAVA_HOMES
environment variable. Note that when using this solution, the JDK that SBT uses is then not necessarily the one identified by your system-wide (or user-specific) JAVA_HOME
environment variable. (If you have not defined JAVA_HOMES
, then SBT will expect you to have defined a valid JAVA_HOME
variable.)
One word of caution: if you commit .java-version
to source control, you must ensure that everyone defines a valid JAVA_HOMES
environment variable, and has a JDK with the exact same name installed in that directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With