Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force SBT to use Java 8?

Tags:

scala

sbt

How can I force SBT to compile to Java 8 class files. I added scalacOptions += "-target:jvm-1.8" but it gives the following error message:

[error] 'jvm-1.8' is not a valid choice for '-target' [error] bad option: '-target:jvm-1.8' [error] (compile:compile) Compilation failed 

I am using SBT version 0.15.5.

I know I am using Java 8 to compile as I added this to build.sbt, but I still wonder why the scalacOptions fails and I don't know what output the scalac produces.

initialize := {   val _ = initialize.value   if (sys.props("java.specification.version") != "1.8")     sys.error("Java 8 is required for this project.") } 
like image 906
Phil Avatar asked Sep 19 '14 03:09

Phil


People also ask

What version of Java does sbt use?

For sbt users, JDK 11 support requires minimum sbt version 1.1. 0. sbt 1.3.

Can sbt be used for Java?

sbt (Simple Build Tool), usually pronounced as es-bee-tee, is a build tool for Scala projects. I found it is also useful to build pure-java projects, but Java programmers are not generally familiar with sbt and Scala.


2 Answers

You need the following on your build.sbt file.

javacOptions ++= Seq("-source", "1.8", "-target", "1.8", "-Xlint")  initialize := {   val _ = initialize.value   val javaVersion = sys.props("java.specification.version")   if (javaVersion != "1.8")     sys.error("Java 1.8 is required for this project. Found " + javaVersion + " instead") } 
like image 56
Renato Avatar answered Nov 08 '22 05:11

Renato


Support in scalac for jvm-1.8 was added in 2.11.4.

Scala version (2.11.2) does not support -target:jvm-1.8 option.

$ scala -version Scala code runner version 2.11.2 -- Copyright 2002-2013, LAMP/EPFL  $ scala -target Usage: -target:<target>  where <target> choices are jvm-1.5, jvm-1.6, jvm-1.7 (default: jvm-1.6)  bad option: '-target'  Usage: scala <options> [<script|class|object|jar> <arguments>]    or  scala -help  All options to scalac (see scalac -help) are also allowed. 
like image 42
Kenji Yoshida Avatar answered Nov 08 '22 05:11

Kenji Yoshida