score:14
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.
Source: stackoverflow.com
Related Query
- How to set up sbt on Windows to select a specific JDK (among available JDKs)?
- How to change JDK set by SBT import in IntelliJ IDEA?
- how to set sbt proxy in windows
- how to set main class in SBT 0.13 project
- Confused how to set up a multi-project sbt project
- How to set the default project for SBT
- How to inform SBT to consume specific scala version for plugins?
- How to make a sbt task use a specific configuration scope?
- How to set SBT default log level to "warn"?
- How to set up managed dependencies in an SBT 0.11 project having Build.scala
- How to set scalacOptions used by SBT when compiling Build.scala?
- How can I provide SBT credentials to my private Artifactory server from a Windows workstation?
- How to set System property for a scalatest from sbt command line
- How to set settings for a subproject in sbt shell (without using project command)?
- How do you make sbt `test` equivalent to a run command with some set of options?
- how to set form value with external select value
- How can one sbt plugin set another plugin's settings then call that other plugin
- How can revisionId in libraryDependencies be the latest available version in SBT 0.13+?
- How can one set a value for all subprojects in sbt shell?
- How to set up multi-module build with reference to external local sbt project?
- playframework - how to set a select input in scala
- How to set Java version for SBT
- how to get specs2 color support on windows using mingw and sbt
- How do I run specific scala-test in sbt cross compile platforms?
- How can I set javaOptions in a custom sbt command
- How to instruct gradle's scala plugin to pick specific JVM among installed ones?
- How to set SBT logLevel for log member of a streams task?
- How to set up jEdit for sbt build error highlighting?
- How to force SBT installer with JDK 1.7 to always use HTTPS protocols that are accepted by Maven?
- How do I set the default system properties for SBT in Windows?
More Query from same tag
- src/main/scala folder disappears on clicking 'update project' in Eclipse
- Lagom's embedded Kafka fails to start after killing Lagom process once
- Scala macros example not working on Scala 2.12.6
- Process the list of different types - is using scala (or functional programming) more expensive than Java?
- Spark - Getting an element after a try call and sucess
- SBT not honouring a custom resolver
- How to create an Enumeratee filter based on a Future[Boolean] instead of a Boolean
- how to pass and return an array in scala
- Scala type mismatch error time conversion program
- Scala Generics with Case Class and Object Companion
- PUT ERROR 404 (Not Found) in playframework with restangular
- How to split string with space and quote both at the same time in scala?
- Collecting dependencies under sbt 0.10 (putting all dependency jars to target/scala-version/lib/)
- Scala message passing order
- How do I import a java classname with a dollar sign ($), or refer to a field or method with one, in Jython?
- How to use reflection in Scala. I am trying to use getDeclaredMethod()
- NoSuchMethodError from dependencies when using spark-submit
- crashed with 'j.u.NoSuchElementException: No attribute named 'notStartedRefIds' is defined'
- Deploy Scala binaries without dependencies
- Interoperability of Scala and Jython
- Why/How does this scala contravariant example work?
- Spark DataFrame - How to partition the data based on condition
- Creating a Writer monad in Scala
- How to select the second smallest element from sorted list?
- SBT before/after hooks for a task
- Scala and OO: Specifying heirachy levels in super class
- Scala - Extend Generic Type Parameter
- Create complex Json Object
- With sudo, write multiple lines to a file using sys.process in Scala
- Adding .jar's to classpath (Scala)