Running java version for Scala project in sbt?

My scala application will only work with Java 7, because it depends on libraries that appeared only in this version of the JDK.

How to do this in sbt so that the correct error message is immediately shown to the user if she uses the wrong version of Java when starting sbt to run / compile the application?

NOTE. For compilation, there is NO Java โ„ข source code. I only have scala source code. Scala code requires import java.nio.file.Path , available since Java 7.

+53
java scala sbt
Oct 06 '13 at
source share
6 answers

Using javacOptions ++= Seq("-source", "1.7", "-target", "1.7") does not work if you do not have Java sources.

But you can set the target JVM for the Scala compiler in build.sbt or Build.scala:

 scalacOptions += "-target:jvm-1.7" 

As a result, he prints on JDK 6:

 $ sbt clean run [info] Set current project to default-cd5534 (in build file:/tmp/so/) [success] Total time: 0 s, completed 27.10.2013 14:31:43 [info] Updating {file:/tmp/so/}default-cd5534... [info] Resolving org.fusesource.jansi#jansi;1.4 ... [info] Done updating. [info] Compiling 1 Scala source to /tmp/so/target/scala-2.10/classes... [info] Running Main [error] (run-main) java.lang.UnsupportedClassVersionError: Main : Unsupported major.minor version 51.0 java.lang.UnsupportedClassVersionError: Main : Unsupported major.minor version 51.0 at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:634) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:277) at java.net.URLClassLoader.access$000(URLClassLoader.java:73) at java.net.URLClassLoader$1.run(URLClassLoader.java:212) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) at java.lang.ClassLoader.loadClass(ClassLoader.java:321) at java.lang.ClassLoader.loadClass(ClassLoader.java:314) [trace] Stack trace suppressed: run last compile:run for the full output. java.lang.RuntimeException: Nonzero exit code: 1 at scala.sys.package$.error(package.scala:27) [trace] Stack trace suppressed: run last compile:run for the full output. [error] (compile:run) Nonzero exit code: 1 [error] Total time: 4 s, completed 27.10.2013 14:31:47 

Note. Perhaps it only works for the latest version of SBT / Scalac.

+41
Oct 27 '13 at 13:36 on
source share

As Scala code, you can put statements in the assembly definition. sbt defines initialize as a common place for such things, but you can use any settings, including custom ones. For example,

 initialize := { val _ = initialize.value // run the previous initialization val classVersion = sys.props("java.class.version") val specVersion = sys.props("java.specification.version") assert(..., "Java N or above required") } 
+19
Oct 09 '13 at 12:19
source share

For anyone in the future, this is also a good way to do this. It stops execution immediately if it cannot find the correct version of Java:

 initialize := { val _ = initialize.value // run the previous initialization val required = "1.8" val current = sys.props("java.specification.version") assert(current == required, s"Unsupported JDK: java.specification.version $current != $required") } 

You put this in your build.sbt .

+14
Apr 15 '15 at 15:34
source share

SBT 0.13.6 has a new VersionNumber and VersionNumberCompatibility . The careful approach recommended by @MarkHarrah for using this can do the following:

 initialize := { val _ = initialize.value // run the previous initialization val required = VersionNumber("1.8") val curr = VersionNumber(sys.props("java.specification.version")) assert(CompatibleJavaVersion(curr, required), s"Java $required or above required") } ... /** Java specification version compatibility rule. */ object CompatibleJavaVersion extends VersionNumberCompatibility { def name = "Java specification compatibility" def isCompatible(current: VersionNumber, required: VersionNumber) = current.numbers.zip(required.numbers).foldRight(required.numbers.size<=current.โ€Œโ€‹numbers.size)((a,b) => (a._1 > a._2) || (a._1==a._2 && b)) def apply(current: VersionNumber, required: VersionNumber) = isCompatible(current, required) } 
+10
Oct 03 '14 at 10:39
source share

To compile in Java 7, you must add the javac parameter to compile with source code 1.7.

You must add javacOptions ++= Seq("-source", "1.7") to the SBT build configuration, which can be found in the / project folder.

Here is a link from SBT: http://www.scala-sbt.org/release/docs/Detailed-Topics/Java-Sources.html

+3
Oct 06 '13 at 14:14
source share

Anyway, if you use the eclipse-based scala change settings in

window -> pref - scala compiler -> standard -> target -> jvm-1.7

enter image description here

+1
Sep 06 '16 at 20:54 on
source share



All Articles