Setting up sbt to use Java 7 for compilation? - scala

I'm getting compile errors when running the compile task as the sources reference new classes in java.nio.file package that only appeared in Java 7.
I have the following in build.sbt:
javaHome := Some(file("/opt/jdk/jdk1.7.0"))
fork := true
In 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?

The most reliable (perhaps only) way to do this at the moment it to start SBT with java in the JDK7 folder.
Modify your sbt launcher script; or use this one that allows you to specify Java Home (and so much more!) as command line options.
~/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.

If you use Linux or Mac, another possibility is to look at jenv, a command line Java manager.
It allows you to choose per project which JDK to use.

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.
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/postactivate so that you define locations for all your tools. You can see an example below which works for me:
#!/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 workon to switch between environments. Example:
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

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=<path-to-jdk-home> sbt
This works for me on OSX with sbt 0.13.8

change javacOption to 1.7? I don't think setting the javaHome is necessary.

Related

Installing sbt on Windows 10 for Scala course

Instructions for the course say to use verion 0.13.x.
I installed the latest msi from the sbt site, but when I type "sbt about", I get:
Microsoft Windows [Version 10.0.15063]
(c) 2017 Microsoft Corporation. All rights reserved.
C:\Users\reall>sbt about
Error: Unable to access jarfile
Copying runtime jar.
The filename, directory name, or volume label syntax is incorrect.
Error: Unable to access jarfile
"C:\Users\reall\.sbt\preloaded\org.scala-sbt\sbt\"1.0.2"\jars\sbt.jar"
Java HotSpot(TM) 64-Bit Server VM warning: Ignoring option MaxPermSize; support was removed in 8.0
[info] Loading project definition from C:\Users\reall\project
[info] Set current project to reall (in build file:/C:/Users/reall/)
[info] This is sbt 1.0.2
[info] The current project is {file:/C:/Users/reall/}reall 0.1-SNAPSHOT
[info] The current project is built against Scala 2.12.3
[info] Available Plugins: sbt.plugins.IvyPlugin, sbt.plugins.JvmPlugin, sbt.plugins.CorePlugin, sbt.plugins.JUnitXmlReportPlugin, sbt.plugins.Giter8TemplatePlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.12.3
i.e., a jar file error and the sbt version 1.0.2.
Any idea what I'm doing wrong?
You are not doing anything wrong, it's just that the required version 0.13.x is not the latest anymore. So you can either follow #dmytro-mitin's answer and reinstall sbt, or you can still use the one you already have: what you installed now is the sbt launcher, it can be used to run different versions of sbt depending on a project. So it's not important which launcher version you are using (unless you're working on something very sbt-specific).
Normally, every sbt project has a project/build.properties file with the sbt version that is needed to work with it:
sbt.version=0.13.16
So you can change (or create) this file and when you run sbt in the project root folder, it will launch sbt version 0.13.16.
Another way to launch specific version of sbt is to run it with the -sbt-version option :
sbt -sbt-version 0.13.16
or using -D flag:
sbt -Dsbt.version=0.13.16
which has exactly the same effect as editing project/build.properties.
Install not the latest version. The latest one is 1.0.2.
Install 0.13.16.
You can download it here: http://www.scala-sbt.org/download.html
There are msi and zip files.
Installing sbt on Windows

sbt [error] No valid parser available

I am running:
java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
sbt 0.13.13.
on Windows 10.
When I follow the tutorial to do a println("hello"), I get the error:
[error] No valid parser available.
Do you guys have any idea why that might be and how to resolve?
Thank you very much in advance!
You entered into the sbt shell, which is used to give commands for a project build.
It sounds like you want a Scala console. You can start it by:
sbt console

Template not found for: -Dsbt.version 0.13.13 lagom/lagom-scala.g8

Good afternoon,
I am trying to follow this hello world tutorial of lagom, however when running the command:
sbt new -Dsbt.version=0.13.13 lagom/lagom-scala.g8
I get the following error:
Template not found for: -Dsbt.version 0.13.13 lagom/lagom-scala.g8
As stated in the setup part of the tutorial I have checked my java version:
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
My javac version:
javac 1.8.0_131
And my sbt version:
[info] 0.13.15
I have trying running the command with my sbt version as the parameter:
sbt new -Dsbt.version=0.13.13 lagom/lagom-scala.g8
But it still throws the no template error, could you please point out what am I doing wrong?
Which comply with the prerequisites for the tutorial
Turns out I had to remove the -Dsbt.version=0.13.13 parameter of the commmand.
Running it as:
sbt new lagom/lagom-scala.g8
Removes the error and it works correctly.
The problem was not the extra flag. The documentation is correct.
You have to use it before the new command
sbt -Dsbt.version=0.13.13 new lagom/lagom-scala.g8

Missing dependencies in Apache Crunch Scala build

I'm trying to build the Apache Crunch source code on my CentOS 7 machine, but am getting the following error in the crunch-spark project when I execute mvn package:
[ERROR] /home/bwatson/programming/git/crunch/crunch-spark/src/it/scala/org/apache/crunch/scrunch/spark/PageRankClassTest.scala:71: error: bad symbolic reference. A signature in PTypeH.class refers to term protobuf
[ERROR] in package com.google which is not available.
[ERROR] It may be completely missing from the current classpath, or the version on
[ERROR] the classpath might be incompatible with the version used when compiling PTypeH.class.
[ERROR] .map(line => { val urls = line.split("\\t"); (urls(0), urls(1)) })
[ERROR] ^
Other SO questions about similar errors (here and here) seem to involve PATH or version issues. I've been messing around but can't seem to resolve them. For completeness:
[bwatson#ben-pc crunch]$ scala -version
Scala code runner version 2.11.5 -- Copyright 2002-2013, LAMP/EPFL
[bwatson#ben-pc crunch]$ 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)
[bwatson#ben-pc crunch]$ mvn -version
Apache Maven 3.0.5 (Red Hat 3.0.5-16)
Maven home: /usr/share/maven
Java version: 1.8.0_31, vendor: Oracle Corporation
Java home: /usr/java/jdk1.8.0_31/jre
Default locale: en_GB, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-123.20.1.el7.x86_64", arch: "amd64", family: "unix"
Any advice? I'm not really sure where Scala is looking for its dependencies, but I'd have thought that Maven would take care of it.
Unfortunately Different versions of Scala are binary incompatible. Currently by default Apache Spark uses Scala 2.10.4, not Scala 2.11. Apache Scrunch is dependent on Spark. Maven does not know anything about this so it can't help. It is necessary to make some modifications to Scrunch to get it to compile for Scala 2.11 / JDK 1.8. I am working on this at the moment, but I don't have a solution yet. However I get the error message you report if I compile Scala 2.10.4 with JDK 1.8, not Scala 2.11, so I don't think it is doing quite what you intend. The error seems be coming from the Protobuf compiler or jar but I don't know why that is.
When I solve it myself, I will report back!
It turns out the official documentation for Crunch was missing a Maven parameter. The issue was solved by building using:
mvn package -Dcrunch.platform=2

Which version of Java does SBT use?

How to find out, which version of JDK SBT uses?
My laptop has both JDK 1.6 and JDK 1.7 installed, so I was just wondering.
You can use eval at the sbt prompt with the appropriate system properties:
> eval System.getProperty("java.version")
[info] ans: String = 1.7.0_45
> eval System.getProperty("java.home")
[info] ans: String = /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/jre
(The other answers are fine too, this is just another method.)
Just run sbt console and you'll get something like this:
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_45).
Type in expressions to have them evaluated.
Type :help for more information.
Which shows you the version of Java being used.
Cheers
JC
It's actually easy.
Find the command line of SBT (cat `which sbt`) and see which Java it is.
For example, my SBT command line begins with:
/usr/bin/java -Xmx512M
And /usr/bin/java -version tells the Java version, in my case it is 1.7.
sbt -v is another way:
# sbt -v
[process_args] java_version = '1.7.0_121'
# Executing command line:
java
-Xms1024m
-Xmx1024m
-XX:ReservedCodeCacheSize=128m
-XX:MaxPermSize=256m
-jar
/usr/share/sbt-launcher-packaging/bin/sbt-launch.jar
[info] Loading project definition from /root/.sbt/0.13/staging/a6d8e5030b69785a9763/build/project
[info] Set current project to xx (in build file:/build/)
>
Here's a batch file method I created to get that info immediately from the command line (Windows only, but you can do something similar in a shell script):
#echo off
setlocal
sbt "eval System.getProperty(\"java.version\")" "eval System.getProperty(\"java.home\")"
#echo on