Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enforcing Java version for Scala project in sbt?

Tags:

java

scala

sbt

My scala application will only run with Java 7 as it depends on libraries that only appeared in that version of the JDK.

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

NOTE: There is NO Java™ source code to compile here. I only have Scala source code. The Scala code requires an import java.nio.file.Path that's available from Java 7.

like image 857
Henry Story Avatar asked Oct 06 '13 12:10

Henry Story


People also ask

What Java version does sbt use?

For sbt users, JDK 11 support requires minimum sbt version 1.1. 0. sbt 1.3. 9 or newer is recommended.

Which version of Scala does sbt use?

sbt 0.13 uses Scala 2.10.

Can sbt be used with Java?

sbt is built for Scala and Java projects. It is the build tool of choice for 93.6% of the Scala developers (2019).

Does Scala use JRE?

HotSpot 1.6 is the standard JRE we use for Scala, and should be the most stable.


1 Answers

Using javacOptions ++= Seq("-source", "1.7", "-target", "1.7") does not work if you have no 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 it prints on a 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: Maybe it works only for the latest SBT/Scalac version.

like image 159
Schleichardt Avatar answered Sep 28 '22 01:09

Schleichardt