How can I force which Scala version SBT should use? - scala

I keep getting the error
Error while loading AnnotatedElement, class file
'/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/rt.jar(java/lang/reflect/AnnotatedElement.class)'
is broken
When running SBT against my project. It appears this error is due to this problem with Scala 2.10 not working with JDK 8. I do have scala 2.11 set and my SCALA_HOME points to Scala 2.11 at "SCALA_HOME=/Library/Scala/scala-2.11.2".
So any idea how to force SBT to use this version or check what version it is currently trying to use?

In your Build.sbt change the Scala version specified to the version you want (it defaults to what SBT was built with)
scalaVersion := "2.11.2"
http://www.scala-sbt.org/release/docs/Howto-Scala.html

Add to the build.sbt this line:
scalaVersion := "2.11.2"

Related

SBT, Scala version mismatch

i have installed Scala 2.11.8 version, and i installed SBT 0.13.12 version as well.
when i create one directory and inside that directory typed sbt and sbt prompt opened. Inside SBT prompt, when i checked libraryDependencies i found like this:
sbt:sparkp> libraryDependencies
[info] * org.scala-lang:scala-library:2.12.4
But in machine, i have installed scala 2.11.8 version, when i checked scala -version it shows
hadoop#localhost:~$ scala -version
Scala code runner version 2.11.8 -- Copyright 2002-2016, LAMP/EPFL
Did i made any mistake while installing sbt? or i need to change any configuration properties after installation?
please help out.
Note: There are some questions regarding this error in stackoverflow, but i didn't get exact answer that's why i am asking new question.
In my opinion the scala version you are getting is the version with which sbt is built with.
The solution to your is to find global.sbt file which is usually in ~/.sbt/<version>/global.sbt
and add following line
scalaVersion := "2.11.8"
Your problem should be solved.
The Scala version of a project in SBT is completely unrelated to one you have installed and available from command line. This is a very good thing for several reasons:
Everyone who builds a project doesn't get different result depending on Scala version they have installed.
You don't need to install multiple Scala versions and switch between them all the time to work on multiple projects.
Many projects should be built with multiple Scala versions, see http://www.scala-sbt.org/0.13/docs/Cross-Build.html.
If you are creating a new project, you should specify scalaVersion := "2.11.8" (or any other you want) in build.sbt in this project.

sbt configuring -- or using an already installed -- newer version of Scala

When I install the latest version of sbt (v0.13.9) and then run the following to download scala and associated libraries:
jdoe$ sbt test
jdoe$
jdoe$ sbt console
[info] Set current project to nmvega (in build file:/home/jdoe/)
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_31).
Type in expressions to have them evaluated.
Type :help for more information.
scala>
I get Scala version 2.10.5, as seen above.
How can I get sbt to build with (or use a separately installed) scala version, say v2.12.0-M3 ? I can't find instructions for this.
Thank you in advance.
As you can see in documentation SBT provides many ways to Configure and use Scala
Set the Scala version used for building the project
The scalaVersion configures the version of Scala used for compilation. By default, sbt also adds a dependency on the Scala library with this version. See the next section for how to disable this automatic dependency. If the Scala version is not specified, the version sbt was built against is used. It is recommended to explicitly specify the version of Scala.
For example, to set the Scala version to "2.9.2",
scalaVersion := "2.9.2"
Disable the automatic dependency on the Scala library
sbt adds a dependency on the Scala standard library by default. To disable this behavior, set the autoScalaLibrary setting to false.
autoScalaLibrary := false
Temporarily switch to a different Scala version
To set the Scala version in all scopes to a specific value, use the ++ command. For example, to temporarily use Scala 2.8.2, run:
> ++ 2.8.2
Use a local Scala installation for building a project
Defining the scalaHome setting with the path to the Scala home directory will use that Scala installation. sbt still requires scalaVersion to be set when a local Scala version is used. For example,
scalaVersion := "2.10.0-local"
scalaHome := Some(file("/path/to/scala/home/"))
Can you post your build.sbt please.
You should be able to declare the Scala by adding the following to build.sbt file
scalaVersion := "2.11.7"

How to make intellij import scala project with scala 2.11.7 (as per build.sbt)?

I'm having problems with my project which I think are due to intellij pulling in a load of scala 2.10 libraries when it created/if I reimport my project.
How can I configure it to either read the version of scala from build.sbt, or manually configure it to compile with scala 2.11.7? I'm running intellij IDEA 14 with the latest updates and it always uses scala 2.10.4.
Make sure you have your Scala Version set in your build.sbt
scalaVersion := "2.11.7"
The rest works seemlessly. Enable "Auto-Import" in IDEA when creating the project.
The 2.10 libraries are used by SBT itself. If you check the Dependencies tab for your modules, you shouldn't see them. If you do, you likely have an error in libraryDependencies in build.sbt.

How do I tell sbt to use local scala installation?

Trying to learn how to use sbt and stuck with a situation: when I install sbt and run it for the first time, it tries to download scala 2.9.x into some directory inside my home. I have scala 2.10.2 installed somewhere else, so how do I tell sbt to use that scala distribution?
UPD.: Solution (this is distribution for sbt to use when building projects, but sbt will anyway download scala distribution needed for it itself):
***#***:~|⇒ cat .sbt/global.sbt
scalaVersion := "2.10.2"
scalaHome := Some(file("/usr/share/scala"))
You could copy the .jars of your distribution to ~/.ivy2/cache. But that would be totaly missing the point of using sbt. If you want to use scala 2.10.2, just put
scalaVersion := "2.10.2"
into your build.sbt, and it will download this version for you. Then if you want to update to 2.11 when it comes out, all you have to do is change a single line in your build.sbt.

how to configure 2.9.1 in org.scala-sbt#sbt_2.9.1;0.12.3

I am trying a very simple sbt example; when I compile it with sbt, and always get the following error:
org.scala-sbt#sbt_2.9.1;0.12.3: not found
I found a build.properties file under project folder, where I could change the 0.12.3 part; for example, after I changed it to 0.11.3, it will succeed until another inompatible issue; However, I want to know how to change sbt_2.9.1 to, say, sbt_2.9.2; I don't find a configuration file, and even I update the sbt to the latest version 0.12.3, still no luck.
my build.sbt file:
organization := "com.typesafe.slick"
name := "slick-examples"
version := "1.0.1-RC1"
scalaVersion := "2.10.1"
scalacOptions += "-deprecation"
anyone please help me.
The Scala version sbt is using internally and the one used for your project are totally independent. Which sbt launcher are you using? Make sure you are using an sbt.version property that works for your sbt launcher.
Again, no need to configure the Scala version for your project at that level. Write a build.sbt file and set scalaVersion to, e.g. 2.10.1 (assuming you use sbt 0.12.x): scalaVersion := 2.10.1