Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
In this tutorial, we’ll see how to get the current Scala version being used in our application.
There are different ways to get the current Scala version programmatically.
We can access properties on the scala.util.Properties object:
scala> util.Properties.versionNumberString
res0: String = 2.12.17
scala> util.Properties.versionString
res1: String = version 2.12.17
scala> util.Properties.versionMsg
res2: String = Scala library version 2.12.17 -- Copyright 2002-2022, LAMP/EPFL and Lightbend, Inc.
These three solutions look into the scala.util.Properties object which contains several different properties we can use. Depending on our goal, we may want to display just the version number, prefixing it with the version keyword, or even an entire version message.
The Scala compiler API also provides a solution to get the Scala version. This method is designed mainly for compiler plugin development:
scala> scala.tools.nsc.Properties.versionNumberString
res0: String = 2.12.17
scala> scala.tools.nsc.Properties.versionString
res1: String = version 2.12.17
scala> scala.tools.nsc.Properties.versionMsg
res2: String = Scala compiler version 2.12.17 -- Copyright 2002-2022, LAMP/EPFL and Lightbend, Inc.
This is very similar to the previous solutions we saw.
In this article, we saw how to get the current Scala version programmatically using both the scala.util.Properties object and the Scala compiler Properties object.