scala 设置 sbt 以使用 Java 7 进行编译?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7701692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting up sbt to use Java 7 for compilation?
提问by Nick A Miller
I'm getting compile errors when running the compiletask as the sources reference new classes in java.nio.filepackage that only appeared in Java 7.
运行compile任务时出现编译错误,因为源引用了java.nio.file包中仅出现在 Java 7 中的新类。
I have the following in build.sbt:
我有以下内容build.sbt:
javaHome := Some(file("/opt/jdk/jdk1.7.0"))
fork := true
In sbt:
在 sbt:
> show java-home
[info] Some(/opt/jdk/jdk1.7.0)
It compiles and runs fine in Eclipse. How can I set up sbt to use Java 7 for compilation?
它在 Eclipse 中编译并运行良好。如何设置 sbt 以使用 Java 7 进行编译?
回答by retronym
The most reliable (perhaps only) way to do this at the moment it to start SBT with javain the JDK7 folder.
目前最可靠(可能是唯一)的方法是java在 JDK7 文件夹中启动 SBT 。
Modify your sbtlauncher script; or use this onethat allows you to specify Java Home (and so much more!) as command line options.
修改你的sbt启动脚本;或者使用这个允许您指定 Java Home(以及更多!)作为命令行选项的选项。
~/code/scratch/20111009 sbt -java-home /Library/Java/JavaVirtualMachines/openjdk-1.7-x86_64/Contents/Home
Starting sbt: invoke with -help for other options
[info] Loading global plugins from /Users/jason/.sbt/plugins
[info] Set current project to default-3e990a (in build file:/Users/jason/code/scratch/20111009/)
> console
[info] Compiling 1 Scala source to /Users/jason/code/scratch/20111009/target/scala-2.9.1/classes...
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.9.1.final (OpenJDK 64-Bit Server VM, Java 1.7.0-internal).
Type in expressions to have them evaluated.
Type :help for more information.
scala> java.util.Objects.equals(null, null)
res0: Boolean = true
Simply setting javaHome := Some(file("/Library/Java/JavaVirtualMachines/openjdk-1.7-x86_64/Contents/Home"))changes the Java version used to compile and fork processes, but does not change the version of the Java standard library on the classpath, nor the version used to run tests, which are always run the the same JVM as SBT.
只需设置javaHome := Some(file("/Library/Java/JavaVirtualMachines/openjdk-1.7-x86_64/Contents/Home"))更改用于编译和 fork 进程的 Java 版本,但不会更改类路径上的 Java 标准库版本,也不会更改用于运行测试的版本,这些版本始终与 SBT 运行相同的 JVM。
回答by Gildas Cuisinier
回答by Richard Gomes
I use virtualenv, which is a tool from the Python ecosystem. In a nutshell, it is a shell script which allows you to change your PATH variable easily and get back to what it was before, if you need to.
我使用virtualenv,它是 Python 生态系统中的一个工具。简而言之,它是一个 shell 脚本,它允许您轻松更改 PATH 变量并在需要时恢复到以前的状态。
First install virtualenvwrapper (a wrapper around virtualenv):
$ apt-get install virtualenvwrapper
Now create a virtual environment for, say, Java8 with Scala-2.11.
$ mkvirtualenv j8s11
Now, adjust ~/.virtualenvs/j8s11/bin/postactivateso that you define locations for all your tools. You can see an example below which works for me:
首先安装 virtualenvwrapper (一个围绕virtualenv的包装器):
$ apt-get install virtualenvwrapper
现在使用 Scala-2.11 为 Java8 创建一个虚拟环境。
$ mkvirtualenv j8s11
现在,调整~/.virtualenvs/j8s11/bin/postactivate以便为所有工具定义位置。您可以在下面看到一个对我有用的示例:
#!/bin/bash
JAVA_VERSION=1.8.0_31
SCALA_VERSION=2.11.5
SBT_VERSION=0.13.7
ANT_VERSION=1.9.4
M2_VERSION=3.2.5
GRADLE_VERSION=1.6
PLAY_VERSION=2.3.7
ACTIVATOR_VERSION=1.2.12
IDEA_VERSION=IC-135.475
PYCHARM_VERSION=community-3.4.1
TOOLS_HOME=/opt/developer
export JAVA_HOME=${TOOLS_HOME}/jdk${JAVA_VERSION}
export SCALA_HOME=${TOOLS_HOME}/scala-${SCALA_VERSION}
export SBT_HOME=${TOOLS_HOME}/sbt-${SBT_VERSION}
export ANT_HOME=${TOOLS_HOME}/apache-ant-${ANT_VERSION}
export M2_HOME=${TOOLS_HOME}/apache-maven-${M2_VERSION}
export GRADLE_HOME=${TOOLS_HOME}/gradle-${GRADLE_VERSION}
export PLAY_HOME=${TOOLS_HOME}/play-${PLAY_VERSION}
export ACTIVATOR_HOME=${TOOLS_HOME}/activator-${ACTIVATOR_VERSION}
export IDEA_HOME=${TOOLS_HOME}/idea-${IDEA_VERSION}
export PYCHARM_HOME=${TOOLS_HOME}/pycharm-${PYCHARM_VERSION}
PATH=${PYCHARM_HOME}/bin:$PATH
PATH=${IDEA_HOME}/bin:$PATH
PATH=${ACTIVATOR_HOME}:$PATH
PATH=${PLAY_HOME}:$PATH
PATH=${GRADLE_HOME}/bin:$PATH
PATH=${M2_HOME}/bin:$PATH
PATH=${ANT_HOME}/bin:$PATH
PATH=${SBT_HOME}/bin:$PATH
PATH=${SCALA_HOME}/bin:$PATH
PATH=${JAVA_HOME}/bin:$PATH
export PATH
- Now you can just use workonto switch between environments. Example:
- 现在您可以使用workon在环境之间切换。例子:
rgomes@terra:~$ workon j8s11 (j8s11)rgomes@terra:~$ java -version java version "1.8.0_31" Java(TM) SE Runtime Environment (build 1.8.0_31-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode) (j8s11)rgomes@terra:~$ scala -version Scala code runner version 2.11.5 -- Copyright 2002-2013, LAMP/EPFL (j8s11)rgomes@terra:~$ workon j7s10 (j7s10)rgomes@terra:~$ java -version java version "1.7.0_71" Java(TM) SE Runtime Environment (build 1.7.0_71-b14) Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode) (j7s10)rgomes@terra:~$ scala -version Scala code runner version 2.10.4 -- Copyright 2002-2013, LAMP/EPFL
回答by Cpt. Senkfuss
I'm assuming you want to change whatever you have set in JAVA_HOME by default, which you can do when invoking sbt:
我假设您想更改默认情况下在 JAVA_HOME 中设置的任何内容,您可以在调用 sbt 时执行此操作:
JAVA_HOME=<path-to-jdk-home> sbt
This works for me on OSX with sbt 0.13.8
这在 sbt 0.13.8 的 OSX 上对我有用
回答by DarrenWang
change javacOption to 1.7? I don't think setting the javaHome is necessary.
将 javacOption 更改为 1.7?我认为没有必要设置 javaHome。

