14

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.

Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
Hannes
  • 5,002
  • 8
  • 31
  • 60
  • 1
    Please refer https://stackoverflow.com/questions/7701692/setting-up-sbt-to-use-java-7-for-compilation and https://stackoverflow.com/questions/30286058/how-to-specify-which-java-version-to-use-with-sbt-0-12-4. They might be exactly what you are looking for. – Chaitanya Jun 02 '19 at 19:34
  • @ChaitanyaWaikar Are you sure that those are the only available options? There is really no command line property out of the box? I cannot be the only developer who has this situation. – Hannes Jun 02 '19 at 19:51
  • @ChaitanyaWaikar Addition: To me that mentioned `sbt-extras` look as it was only for Unix/Linux systems but not for Windows. – Hannes Jun 02 '19 at 20:00
  • You can set env variable like `JAVA_HOME` for your current shell only. You don't have to set it globally in your system. `set JAVA_HOME=c:\` and then run `sbt` from the same shell. When you close the shell the env variable will be gone so you need to do it every time you run sbt in a new shell. – yǝsʞǝla Jun 03 '19 at 08:53
  • Just to be clear, are you trying to specify the _JDK_ used to run _SBT_, or the _JDK_ used to run your project and/or tests from within _SBT_? – Mike Allen Jun 03 '19 at 18:44
  • @MikeAllen I am not exactly sure. All I want is that sbt uses a specific JDK to compile and run my application. – Hannes Jun 03 '19 at 19:47
  • 1
    OK. By default, _SBT_ uses the same _JDK_ for running your application as it uses itself. My answer below should be OK for you... – Mike Allen Jun 03 '19 at 19:49

1 Answers1

15

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.

Mike Allen
  • 8,139
  • 2
  • 24
  • 46
  • 1
    Very interesting solution. Didn't find any reference on official docs so thanks for detailed answer. – Frankie Nov 01 '19 at 20:11
  • This does not appear to be supported _in general_; being unique/specific to windows and sbt.bat. – Richard Sitze May 14 '21 at 21:20
  • 1
    @RichardSitze: The question was "How to set up sbt on Windows..." You can use _jenv_ on other platforms; I just explained how to specify the JDK on _Windows_. – Mike Allen May 15 '21 at 03:31
  • @MikeAllen That was understood, was clear from the title, and implied by your reference to sbt.bat. It's a very helpful response. That said, I don't typically see tools providing platform specific solutions (though arguably necessary/helpful for windows), and felt it deserved to be clearly stated. – Richard Sitze May 16 '21 at 04:19
  • 1
    This lead me to jenv, which looks to be working! :) – combinatorist Aug 02 '21 at 21:57
  • This helped me find jenv, which worked great on a linux server! :) – combinatorist Aug 03 '21 at 14:53