1. Overview

In this tutorial, we’ll see how to get the current Scala version being used in our application.

2. Get the Current Scala Version

There are different ways to get the current Scala version programmatically.

2.1. Using scala.util.Properties

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.

2.2. Using the Compiler API

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.

3. Conclusion

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.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.