Configuring sbt to use Java 7 to compile?

I get compilation errors when running the compile task, because the sources refer to new classes in the java.nio.file package that appeared only in Java 7.

In build.sbt , I have the following:

 javaHome := Some(file("/opt/jdk/jdk1.7.0")) fork := true 

In sbt:

 > show java-home [info] Some(/opt/jdk/jdk1.7.0) 

It compiles and works fine in Eclipse. How can I configure sbt to use Java 7 for compilation?

+37
scala sbt
Oct 09 2018-11-11T00:
source share
5 answers

The most reliable (possibly the only) way to do this is when he starts SBT with java in the JDK7 folder.

Change the sbt script launcher; or use this one that allows you to specify Java Home (and much more!) as command line options.

 ~/code/scratch/20111009 sbt -java-home /Library/Java/JavaVirtualMachines/openjdk-1.7-x86_64/Contents/Home Starting sbt: invoke with -help for other options [info] Loading global plugins from /Users/jason/.sbt/plugins [info] Set current project to default-3e990a (in build file:/Users/jason/code/scratch/20111009/) > console [info] Compiling 1 Scala source to /Users/jason/code/scratch/20111009/target/scala-2.9.1/classes... [info] Starting scala interpreter... [info] Welcome to Scala version 2.9.1.final (OpenJDK 64-Bit Server VM, Java 1.7.0-internal). Type in expressions to have them evaluated. Type :help for more information. scala> java.util.Objects.equals(null, null) res0: Boolean = true 

Just setting javaHome := Some(file("/Library/Java/JavaVirtualMachines/openjdk-1.7-x86_64/Contents/Home")) changes the version of Java used for compilation and fork processes, but does not change the version of the standard Java library in the path to classes, as well as the version used to run tests that always run in the same JVM as SBT.

+44
Oct 09 '11 at 17:08
source share

If you're using Linux or Mac, another option is to look at jenv , the Java manager on the command line.

It allows you to choose one project that the JDK should use.

+4
Feb 25 '13 at 12:15
source share

I am using virtualenv, which is a tool from the Python ecosystem. In a nutshell, this is a shell script that allows you to easily modify your PATH variable and go back to what it was before, if you need to.

  • First install virtualenvwrapper (wrapper around virtualenv):

    $ apt-get install virtualenvwrapper

  • Now create a virtual environment like Java8 with Scala -2.11.

    $ mkvirtualenv j8s11

  • Now configure ~ / .virtualenvs / j8s11 / bin / postactivate to determine the locations for all of your tools. Below is an example that works for me:

 #! / bin / bash

 JAVA_VERSION = 1.8.0_31
 SCALA_VERSION = 2.11.5
 SBT_VERSION = 0.13.7
 ANT_VERSION = 1.9.4
 M2_VERSION = 3.2.5
 GRADLE_VERSION = 1.6
 PLAY_VERSION = 2.3.7
 ACTIVATOR_VERSION = 1.2.12
 IDEA_VERSION = IC-135.475
 PYCHARM_VERSION = community-3.4.1

 TOOLS_HOME = / opt / developer
 export JAVA_HOME = $ {TOOLS_HOME} / jdk $ {JAVA_VERSION}
 export SCALA_HOME = $ {TOOLS_HOME} / scala - $ {SCALA_VERSION}
 export SBT_HOME = $ {TOOLS_HOME} / sbt - $ {SBT_VERSION}
 export ANT_HOME = $ {TOOLS_HOME} / apache-ant - $ {ANT_VERSION}
 export M2_HOME = $ {TOOLS_HOME} / apache-maven - $ {M2_VERSION}
 export GRADLE_HOME = $ {TOOLS_HOME} / gradle - $ {GRADLE_VERSION}
 export PLAY_HOME = $ {TOOLS_HOME} / play - $ {PLAY_VERSION}
 export ACTIVATOR_HOME = $ {TOOLS_HOME} / activator - $ {ACTIVATOR_VERSION}
 export IDEA_HOME = $ {TOOLS_HOME} / idea - $ {IDEA_VERSION}
 export PYCHARM_HOME = $ {TOOLS_HOME} / pycharm - $ {PYCHARM_VERSION}

 PATH = $ {PYCHARM_HOME} / bin: $ PATH
 PATH = $ {IDEA_HOME} / bin: $ PATH
 PATH = $ {ACTIVATOR_HOME}: $ PATH
 PATH = $ {PLAY_HOME}: $ PATH
 PATH = $ {GRADLE_HOME} / bin: $ PATH
 PATH = $ {M2_HOME} / bin: $ PATH
 PATH = $ {ANT_HOME} / bin: $ PATH
 PATH = $ {SBT_HOME} / bin: $ PATH
 PATH = $ {SCALA_HOME} / bin: $ PATH
 PATH = $ {JAVA_HOME} / bin: $ PATH
 export PATH
  1. Now you can simply use workon to switch between environments. Example:
 rgomes @ terra: ~ $ workon j8s11

 (j8s11) rgomes @ terra: ~ $ java -version
 java version "1.8.0_31"
 Java (TM) SE Runtime Environment (build 1.8.0_31-b13)
 Java HotSpot (TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
 (j8s11) rgomes @ terra: ~ $ scala -version
 Scala code runner version 2.11.5 - Copyright 2002-2013, LAMP / EPFL

 (j8s11) rgomes @ terra: ~ $ workon j7s10

 (j7s10) rgomes @ terra: ~ $ java -version
 java version "1.7.0_71"
 Java (TM) SE Runtime Environment (build 1.7.0_71-b14)
 Java HotSpot (TM) 64-Bit Server VM (build 24.71-b01, mixed mode)
 (j7s10) rgomes @ terra: ~ $ scala -version
 Scala code runner version 2.10.4 - Copyright 2002-2013, LAMP / EPFL
+3
Feb 21 '15 at 13:03
source share

I assume that you want to change everything that you set in JAVA_HOME by default, which you can do when you call sbt:

 JAVA_HOME=<path-to-jdk-home> sbt 

This works for me on OSX with sbt 0.13.8

+2
May 18 '15 at 9:27
source share

change javacOption to 1.7? I don't think javaHome configuration is necessary.

-2
Oct 10 '11 at 0:22
source share



All Articles