76

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.

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
Henry Story
  • 2,116
  • 1
  • 17
  • 28

6 Answers6

59

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.

Schleichardt
  • 7,502
  • 1
  • 27
  • 37
30

For anybody in the future, this is also a good way to do it. It halts execution immediately if it cannot find the right Java version:

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.

Community
  • 1
  • 1
papacharlie
  • 356
  • 3
  • 8
22

Being Scala code, you can put assertions in the build definition. sbt defines the initialize as a common place for things like this, but you can use any setting, including a custom one. 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")
}
Mark Harrah
  • 6,999
  • 1
  • 26
  • 31
  • 2
    This makes sense. However I am missing the other part: how do I choose which JDK does SBT use? (Somehow my SBT insists on using Java 7, while I need Java 8 libraries). – Suma Mar 22 '16 at 20:45
16

In SBT 0.13.6 there is a new VersionNumber class and VersionNumberCompatibility trait. Tweaking the approach recommended by @MarkHarrah to use this one might 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)
}
metasim
  • 4,793
  • 3
  • 46
  • 70
  • 3
    This `isCompatible` definition does the wrong thing if we ever have a java `2.0`. What you want is something like `current.numbers.zip(required.numbers).foldRight(required.numbers.size<=current.numbers.size)((a,b) => (a._1 > a._2) || (a._1==a._2 && b))` – Daniel Martin Dec 02 '15 at 17:30
  • Thanks much @DanielMartin. Updated with your fix. – metasim Dec 03 '15 at 13:51
  • In `build.sbt` or a custom plugin in `project`. – metasim Jun 06 '19 at 01:46
4

In order to compile in Java 7, you should add the javac option to compile with source 1.7.

You should add javacOptions ++= Seq("-source", "1.7") to your SBT build config that can be found in the /project folder.

Here's the reference from SBT: http://www.scala-sbt.org/release/docs/Detailed-Topics/Java-Sources.html

Andrew Jones
  • 1,382
  • 10
  • 26
  • Well, if one will run this code on 1.6 error messages [wouldn't be much comprehensive](http://stackoverflow.com/q/10382929/298389). – om-nom-nom Oct 06 '13 at 14:21
  • 5
    that section of SBT deals with "support for compiling Java sources" but not for which java VM is used by Scala. Currently I am not compiling any java sources in my scala project, I am just using libraries that are not available in jdk6, and only available in jdk7 – Henry Story Oct 06 '13 at 14:55
3

Just in-case if you use eclipse based scala-ide change settings in

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

enter image description here

Jai
  • 343
  • 1
  • 9