How to specify which java version to use with sbt

2019-09-03 18:37发布

问题:

How to specify which java version to use with sbt 0.12.4? I have java 7 and java 8 installed. Since sbt 0.12.4 does not support java 8 but tries to use it by default, I have to specify java version explicitly for sbt. I want to keep using java 8 as a default version of java for other things.

回答1:

One way that works well for me is, using the sbt script from sbt-extras and add to the root of the project a file called .sbtopts with:

-java-home
/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home

using whatever version of Java 7 you want to use. Make sure that those are on separate.



回答2:

If you're not using the sbt script from sbt-extras you could use jEnv.

After configuring jEnv, from the root of the project run jenv local 1.7, which will create a file called .java-version with content "1.7".

Then every time you run java (or javac, etc) in that project (including from any directory in that project) it will run the 1.6 version, thanks to jEnv's shim mechanisms.