Scala 3: How to create and run a Scala script

As a brief tip today, in Scala 3 you can run a script as follows. First, put this Scala 3 code into a file named Hello.scala:

@main def hello = println("Hello, world")

Next, at your operating system command line, run your script like this:

$ scala Hello.scala
Hello, world

For a little script with no dependencies, that’s all you need to do.

Putting a JAR file on the classpath with your script

If you happen to need a JAR file when running your Scala 3 script, you can run it like this:

scala -cp MyNecessaryJarFile.jar Test.scala

Here in October, 2021, I just confirmed that this works as desired.