110

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?

7
  • 1
    The Scala IDE for Eclipse does not store a path to scalac; it invokes the compiler classes directly from within with Eclipse JVM. May 25, 2011 at 8: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, 2011 at 9:07
  • Damn, someone, please make this a wiki! :)
    – mlvljr
    May 13, 2014 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, 2020 at 12:26
  • 1
    What's the purpose of these comments of yours? Dec 23, 2021 at 11:37

4 Answers 4

139

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
2
  • 6
    I had to add scala as spark libraries were loaded first. So scala.util.Properties.versionString Feb 23, 2017 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 2, 2022 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
2
  • 2
    The first one is nicest of all on this page, seemingly :)
    – mlvljr
    Nov 23, 2014 at 7: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, 2015 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.

3
  • I think you can use the scala.tools.nsc.Properties.versionString anywhere.
    – HairyFotr
    May 25, 2011 at 9:03
  • On import scala.tools.nsc.Properties; mine says "object tools is not a member of package ***.scala" :(
    – mlvljr
    May 25, 2011 at 9:12
  • 7
    scala.tools... is only available, if the scala-compiler.jar is in the classpath. May 25, 2011 at 9:35
9

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.