简体   繁体   中英

sbt with java-home uses default jdk to compile java sources

I have a mixed scala/java project and have a host that has multiple jdks on it. The default java (via alternatives) is jdk8. If I build a project specifying a different java-home, java sources are still built using the default jdk on the path.

$ java -version
java version "1.8.0_20"

/usr/bin/java is linked to /etc/alternatives/java which is linked to /usr/lib/jvm/jdk8/bin/java as per the host's setup.

$ sbt -java-home /usr/lib/jvm/jdk7 compile
....
[warn] Error reading API from class file : java.lang.UnsupportedClassVersionError: my/org/package/SomeJavaClass : Unsupported major.minor version 52.0
....
$ javap -verbose target/scala_2.10/classes/my/org/package/SomeJavaClass.class | grep major
major version: 52
$ javap -verbose target/scala_2.10/classes/my/org/package/SomeScalaClass.class | grep major
major version: 51

As you can see, the java sources were compiled to java 8 (assuming a java process is spawned from the default path), while scala sources used the jdk7 to build. I am able to get the java sources to compile for java 7 by specifying javacOptions in the build definition:

javacOptions ++= Seq("-source","1.7","-target", "1.7")

but I'd like to avoid having to do this in the build source and prefer to build for java 7 at compile time with sbt parameters if possible.

How could I get SBT to use a different java (assuming fork a java process and specify a full path)? Would this be considered an SBT bug if sbt doesn't spawn the javac from the java-home directory?

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

scalacOptions += "-target:jvm-1.7"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM