Scala IDE: Setting version of Scala interpreter - scala

I'm trying to create an RStudio-like experience for Scala and spark. After figuring out how to install it properly (including Scala 2.10.5) on Win7x64, I'm facing the problem, that the version of the interpreter
scala> scala.tools.nsc.Properties.versionString
res1: String = version 2.11.8
differs from the spark-shell scala version
scala> scala.tools.nsc.Properties.versionString
res1: String = version 2.10.5
and from the scala version (cmd.exe)
C:\>scala -version
Scala code runner version 2.10.5 -- Copyright 2002-2013, LAMP,EPFL
Which causes my problems according to this answer problems when starting spark from the interpreter
scala> val sc = new SparkContext(conf)
java.lang.NoSuchMethodError: scala.
collection.immutable.HashSet$.empty()Lscala/collection/immutable/HashSet;
Question: Where do I set/configure the version the interpreter uses? Searching the global settings of the Scala IDE for "interpreter" gave no results. Seems to be something that was shipped along with the Scala IDE?
(spark 1.6.1, hadoop 2.6, Scala IDE 4.4.1, Scala 2.10.5, at least that's the way it should be)

Instead of using a specific scala REPL installation, you can always use sbt console and a build.sbt file to spin up the correct version.
If you add a build.sbt file to your main directory, Scala IDE should pick up on the version you want to be using.
~/test2$ cat build.sbt
scalaVersion := "2.11.7"
~/test2$ sbt console
[info] Set current project to test2 (in build file:/projects/9ab40ff8-31cd-4860-9cc9-ceb67d1afa39/test2/)
[info] Updating {file:/projects/9ab40ff8-31cd-4860-9cc9-ceb67d1afa39/test2/}test2...
[info] Resolving jline#jline;2.12.1 ...
[info] Done updating.
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.11.7 (OpenJDK 64-Bit Server VM, Java 1.7.0_101).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :q
[success] Total time: 24 s, completed Jun 16, 2016 9:18:19 PM
~/test2$ echo 'scalaVersion := "2.10.5"' > build.sbt
~/test2$ cat build.sbt
scalaVersion := "2.10.5"
~/test2$ sbt console
[info] Set current project to test2 (in build file:/projects/9ab40ff8-31cd-4860-9cc9-ceb67d1afa39/test2/)
[info] Updating {file:/projects/9ab40ff8-31cd-4860-9cc9-ceb67d1afa39/test2/}test2...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] 'compiler-interface' not yet compiled for Scala 2.10.5. Compiling...
[info] Compilation completed in 61.292 s
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.5 (OpenJDK 64-Bit Server VM, Java 1.7.0_101).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :q
[success] Total time: 77 s, completed Jun 16, 2016 9:20:45 PM
You can also specify library dependencies from places like mvnrepository and sbt will download them (if not already downloaded) and add them to your REPL classpath.
As a final aside, check out Jupyter Scala for an Ipython-esque notebook interface for Scala.

Related

Using sbt run on simple tutorial example does not execute

On a Windows machine, I am running through the getting started tutorial here: https://www.scala-sbt.org/1.x/docs/sbt-by-example.html
I am able to start the sbt shell and compile but using the run command just hangs and I am forced to Ctrl-C to exit sbt.
Running scala against the JAR file directly also works as expected.
Sample output is the following:
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
[info] Loading project definition from S:\foo-build\project
[info] Loading settings for project foo-build from build.sbt ...
[info] Set current project to foo-build (in build file:/S:/foo-build/)
[info] sbt server started at local:sbt-server-8c6933abcb6848dbd242
sbt:foo-build> about
[info] This is sbt 1.2.8
[info] The current project is ProjectRef(uri("file:/S:/foo-build/"), "foo-build") 0.1.0-SNAPSHOT
[info] The current project is built against Scala 2.12.7
[info] Available Plugins
[info] - sbt.ScriptedPlugin
[info] - sbt.plugins.CorePlugin
[info] - sbt.plugins.Giter8TemplatePlugin
[info] - sbt.plugins.IvyPlugin
[info] - sbt.plugins.JUnitXmlReportPlugin
[info] - sbt.plugins.JvmPlugin
[info] - sbt.plugins.SbtPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.12.7
sbt:foo-build> compile
[success] Total time: 0 s, completed Feb 7, 2019 4:40:45 PM
sbt:foo-build> run
Terminate batch job (Y/N)? Y
This is enough of an answer for my own purposes.
I was able work around this issue by adding a line to my build.sbt file. After adding ThisBuild / scalaVersion := "2.12.8" I was able to use the run command. Following the tutorial linked in the main question I would not expect to have to do this but perhaps someone could shed some light.

Getting "Internal error: Scala instance doesn't exist or is invalid" when trying to run a worksheet in IntelliJ IDEA

I'm trying to get Scala set up for a Coursera course on Ubuntu, and I've followed their instructions by installing open-jdk 1.8, sbt (sbt about says v1.0.2), and IntelliJ Idea. I've set up an sbt project with a HelloWorld Scala worksheet in src/main/scala that just says "Hello, world", but when I try to evaluate the worksheet, I get the error
Internal error: Scala instance doesn't exist or is invalid: version unknown, library jar: /home/[my username]/.ivy2/cache/jline/jline/jars/jline-2.14.5.jar, compiler jar: /home/[my username]/.ivy2/cache/org.scala-lang.modules/scala-xml_2.12/bundles/scala-xml_2.12-1.0.6.jar
followed by a longer trace. Is there a setup step I'm missing? The project seems to point to Java v1.8 in the Project Structure dependencies, and in the "Libraries" tab I see SBT set to Scala v2.12.
I've just had the same problem with macOS Sierra.
I solved after noticing that in my build.sbt I had:
name := "myScalaTests"
version := "0.1"
scalaVersion := "2.12.4"
but prompting sbt about, my scala version was different.
sbt about
[warn] No sbt.version set in project/build.properties, base directory: /Users/myUser/projects
[info] Set current project to projects (in build file:/Users/myUser/projects/)
[info] This is sbt 1.0.2
[info] The current project is {file:/Users/myUser/projects/}projects 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
so I changed the version of scala in the build.sbt accordingly, ant it worked.
The same internal error can be caused by a version mismatch between the sbt launcher and the project sbt version, as described here:
Internal error: Scala instance doesn't exist or is invalid
In my project, there was no scala version mismatch, but downloading the latest sbt launcher and restarting my session resolved the issue.

How do I get "sbt hello, world" to run?

I'm trying to get a clue about Scala/SBT and was walking through http://www.scala-sbt.org/0.13/docs/Hello.html
The first step went well...I've got a directory called "hello" that has the result of $ sbt new sbt/scala-seed.g8 in it.
At the 2nd step, "Running your app", the wheels come off.
$ sbt
[info] Loading project definition from /Users/robert.kuhar/dev/Hello/project
[info] Set current project to Hello (in build file:/Users/robert.kuhar/dev/Hello/)
> run
[info] Compiling 1 Scala source to /Users/robert.kuhar/dev/Hello/target/scala-2.12/classes...
[info] 'compiler-interface' not yet compiled for Scala 2.12.1. Compiling...
[info] Resolving org.scala-sbt#interface;0.13.13 ...
[trace] Stack trace suppressed: run last compile:compileIncremental for the full output.
[error] (compile:compileIncremental) java.lang.reflect.InvocationTargetException
[error] Total time: 1 s, completed Mar 9, 2017 3:34:03 PM
>
The output of the run last compile:compileIncremental is greek to the eyes of a noobie...
...
[info] Resolving org.scala-sbt#interface;0.13.13 ...
java.lang.VerifyError: Uninitialized object exists on backward branch 209
Exception Details:
Location:
scala/collection/immutable/HashMap$HashTrieMap.split()Lscala/collection/immutable/Seq; #249: goto
Reason:
Error exists in the bytecode
...
I'm off the track, in the weeds, and in search of a clue.
I don't know if any of this version stuff matters, but I'm OSX Sierra with a 1.8 JDK and 0.13.13 SBT. There is a Scala 2.12.1 on this machine.
$ sbt sbtVersion
[info] Loading project definition from /Users/robert.kuhar/dev/Hello/project
[info] Set current project to Hello (in build file:/Users/robert.kuhar/dev/Hello/)
[info] 0.13.13
$ java -version
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)
$ scala -version
Scala code runner version 2.12.1 -- Copyright 2002-2016, LAMP/EPFL and Lightbend, Inc.
How do I get the "SBT Hello, World" to run on my dev box?
Here's a way to do it without the sbt new plugin.
Create the following dirs and file in a directory Hello:
├── build.sbt
└── src
└── main
└── scala
└── Hello.scala
Put this in Hello.scala
object Hello extends App{
println("Hello")
}
In Hello/build.sbt add this line:
scalaVersion := "2.11.8
Run it with run
/tmp/Hello $ sbt [16:40:38]
[info] Loading global plugins from /home/brian/.sbt/0.13/plugins
[info] Set current project to hello (in build file:/tmp/Hello/)
> run
[info] Compiling 1 Scala source to /tmp/Hello/target/scala-2.11/classes...
[info] Running Hello
Hello
...
I've had "mixed" results with the g8/giterate templates in the past but that may or may not be your issue. I've used https://github.com/sbt/sbt-fresh and that worked well. If your completely new to Scala and SBT the above code and build setup should get you started.

Scala version not updating

My project used scala version 2.10.2. I am trying to update to 2.11.4. I have updated my build.sbt. But now when I run sbt compile it still shows me Resolving org.scala-lang#scala-library;2.10.2 .... I believe it means that my scala version hasn't been updated. What am I doing wrong?
I hope I have made my question clear. If there is any confusion then please ask. Thanks in advance.
You can always use show to see the value of any setting:
% sbt
[info] Loading project definition from ...
[info] Set current project to ...
> show scalaVersion
[info] 2.12.0-M1
scalaVersion tells you which Scala version your project is built with. You can also verify it using console:
> console
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.12.0-M1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions to have them evaluated.
Type :help for more information.
scala> scala.util.Properties.versionString
res0: String = version 2.12.0-M1
This may be different from the Scala version used to compile your build definition. sbt 0.13 always uses Scala 2.10 for that:
> eval scala.util.Properties.versionString
[info] ans: String = version 2.10.4
You can't change that; it's determined by the sbt version.

How to use Scala interpreter options with SBT?

When using the Scala interpreter, one could start it with an option like:
C:\Users\John>scala -unchecked
Welcome to Scala version 2.9.2 (Java HotSpot(TM) Client VM, Java 1.6.0_32).
Type in expressions to have them evaluated.
Type :help for more information.
scala>
When using sbt, how can one start the Scala interpreter with options ?
The following try will not work:
C:\Users\John\Test Scala Project 1>sbt
[...]
[info] Loading global plugins from C:\Users\John\.sbt\plugins
[info] Set current project to default-8d4ecc (in build file:/C:/Users/John/Tes
t%20Scala%20Project%201/)
> console -unchecked
[error] Expected end of input.
[error] console -unchecked
[error] ^
With Google & Co I could not figure out how to do this from within the sbt shell. Does anyone know ?
dcs#shadowfax:~/github/ConwayLife (master *)$ sbt
[info] Loading global plugins from /home/dcs/.sbt/plugins/project
[info] Loading global plugins from /home/dcs/.sbt/plugins
[info] Loading project definition from /home/dcs/github/ConwayLife/project
[info] Set current project to default-0d85ea (in build file:/home/dcs/github/ConwayLife/)
default-0d85ea:master>set scalacOptions += "-unchecked"
[info] Reapplying settings...
[info] Set current project to default-0d85ea (in build file:/home/dcs/github/ConwayLife/)