111

This is not about command-line compiler options. How do I programmatically obtain the Scala version inside code?

Alternatively, where does the Eclipse Scala plugin v2 store the path to scalac?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
mlvljr
  • 4,066
  • 8
  • 44
  • 61
  • 1
    The Scala IDE for Eclipse does not store a path to `scalac`; it invokes the compiler classes directly from within with Eclipse JVM. – Jean-Philippe Pellet May 25 '11 at 08:19
  • 2
    @Jean-Philippe Pellet Thanks, btw, I've found that `scala.sys.props` references what seems to be scala libraries under something like `sun.boot.class.path -> F:\eclipse\configuration\org.eclipse.osgi\bundles\316\1\.cp\lib\scala-library.jar;` (with `jar`'s `library.proerties` containing `version.number=2.9.0.final`). But since `scala.sys.props` is in there since `2.9.0` only it's pretty much a hint itself :) – mlvljr May 25 '11 at 09:07
  • Damn, someone, please make this a wiki! :) – mlvljr May 13 '14 at 13:57
  • ..heck, this is going to get 100 upvotes this year, probably, let's keep pushing, folks (who'd have known Scala is so convoluted that even things that simple may puzzle ya). – mlvljr May 28 '20 at 12:26
  • Here we go, folks, here we go. – mlvljr Dec 12 '21 at 17:59
  • 1
    What's the purpose of these comments of yours? – Jean-Philippe Pellet Dec 23 '21 at 11:37
  • @Jean-PhilippePellet To summon a true professional who'd know their way around the SO UI to turn this into a community wiki, of course -- but let me know if you were suspecting something else :) – mlvljr Dec 27 '21 at 18:57

4 Answers4

141

This will work without access to scala-compiler.jar:

Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26).
Type in expressions to have them evaluated.
Type :help for more information.

scala> util.Properties.versionString
res0: java.lang.String = version 2.9.1.final
Paul Butcher
  • 10,722
  • 3
  • 40
  • 44
  • 6
    I had to add scala as spark libraries were loaded first. So `scala.util.Properties.versionString` – Francisco López-Sancho Feb 23 '17 at 23:33
  • 1
    On Scala 3 this will print the standard library version which is shared between 2 and 3 and has version 2.x eg: Scala library version 2.13.8 -- Copyright 2002-2021, LAMP/EPFL and Lightbend, Inc. – Somatik Aug 02 '22 at 15:20
58

There are three ways to get the Scala version -

scala> util.Properties.versionNumberString
res103: String = 2.11.4

scala> util.Properties.versionString
res104: String = version 2.11.4

scala> util.Properties.versionMsg
res105: String = Scala library version 2.11.4 -- Copyright 2002-2013, LAMP/EPFL
Naga Vijayapuram
  • 845
  • 7
  • 11
  • 2
    The first one is nicest of all on this page, seemingly :) – mlvljr Nov 23 '14 at 07:30
  • 1
    FYI `util.Properties.versionNumberString` only exist since scala 2.10.x. For scala below 2.10.x, you can use `util.Properties.releaseVersion.getOrElse("unknown version")` to get version number string. – jordom Feb 28 '15 at 20:38
18

You can get the Scala version like this:

scala> scala.tools.nsc.Properties.versionString
res7: java.lang.String = version 2.9.0.final

I don't know the specifics of the plugin, though.

HairyFotr
  • 1,453
  • 1
  • 15
  • 28
10

We can also get installed Scala version

  1. Open command prompt
  2. type Scala
  3. You will get following output:

Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_6 0). Type in expressions to have them evaluated. Type :help for more information.

Chinya
  • 419
  • 1
  • 7
  • 18