简体   繁体   English

带有java-home的sbt使用默认的jdk来编译Java源代码

[英]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. 我有一个混合的scala / java项目,并且有一个主机,上面有多个jdks。 The default java (via alternatives) is jdk8. 默认的Java(通过替代方法)为jdk8。 If I build a project specifying a different java-home, java sources are still built using the default jdk on the path. 如果我建立一个指定其他java-home的项目,则仍将使用路径上的默认jdk构建Java源。

$ 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. /usr/bin/java链接到/etc/alternatives/java ,根据主机的设置,该文件链接到/usr/lib/jvm/jdk8/bin/java

$ 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. 如您所见,Java源代码已编译为Java 8(假定从默认路径生成了java进程),而Scala源代码使用jdk7进行了构建。 I am able to get the java sources to compile for java 7 by specifying javacOptions in the build definition: 我可以通过在构建定义中指定javacOptions来为Java 7编译Java源代码:

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. 但我想避免在构建源中执行此操作,如果可能的话,更喜欢在编译时使用sbt参数为Java 7进行构建。

How could I get SBT to use a different java (assuming fork a java process and specify a full path)? 如何让SBT使用其他Java(假设派生一个Java进程并指定完整路径)? Would this be considered an SBT bug if sbt doesn't spawn the javac from the java-home directory? 如果sbt没有从java-home目录中产生javac,这将被视为SBT错误吗?

you can set the target JVM for the Scala compiler in build.sbt or Build.scala: 您可以在build.sbt或Build.scala中为Scala编译器设置目标JVM:

scalacOptions += "-target:jvm-1.7" scalacOptions + =“ -target:jvm-1.7”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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