Skip to main content

Run

The run command runs your Scala code:

Hello.scala
object Hello {
def main(args: Array[String]): Unit =
println("Hello")
}
scala-cli run Hello.scala
# Hello

This is the default command, so you don’t have to specify it explicitly:

scala-cli Hello.scala
# Hello

Passing arguments​

You can pass arguments to the application or script you're launching after --:

app.sc
println(args.mkString("App called with arguments: ", ", ", ""))
scala-cli app.sc -- first-arg second-arg
# App called with arguments: first-arg, second-arg

Main class​

If your application has multiple main classes, the --main-class option lets you explicitly specify the main class you want to run:

hi.sc
println("Hi")
scala-cli Hello.scala hi.sc --main-class hi_sc

Custom JVM​

--jvm lets you run your application with a custom JVM:

scala-cli Hello.scala --jvm adopt:14

JVMs are managed by coursier, and are based on the index of the jabba command-line tool.

Watch mode​

--watch makes scala-cli watch your code for changes, and re-runs it upon any change:

scala-cli run Hello.scala  --watch
# Hello
# Watching sources, press Ctrl+C to exit.
# Compiling project (Scala 3.1.1, JVM)
# Compiled project (Scala 3.1.1, JVM)
# Hello World
# Watching sources, press Ctrl+C to exit.

Watch mode - revolver​

--revolver mode runs your application in the background and automatically restart it upon any change:

scala-cli run Hello.scala --revolver
# Hello
# Watching sources, press Ctrl+C to exit.
# Compiling project (Scala 3.1.1, JVM)
# Compiled project (Scala 3.1.1, JVM)
# Hello World
# Watching sources, press Ctrl+C to exit.

Scala.js​

Scala.js applications can also be compiled and run with the --js option. Note that this requires node to be installed on your system:

scala-cli Hello.scala --js

See our dedicated Scala.js guide for more information.

Scala Native​

Scala Native applications can be compiled and run with the --native option. Note that the Scala Native requirements need to be installed for this to work, and that Scala Native only supports Linux and macOS at this time and can only use Scala 2.13 and 2.12 for now:

scala-cli Hello.scala --native -S 2.13.6

We have a dedicated Scala Native guide as well.

Scala Scripts​

Scala CLI can also compile and run Scala scripts:

HelloScript.sc
#!/usr/bin/env -S scala-cli shebang

println("Hello world from scala script")
scala-cli run HelloScript.sc
# Hello world from scala script

Our scripts guide provides many more details.

Scala CLI from docker​

Scala applications can also be compiled and run using a docker image with scala-cli, without needing to install Scala CLI manually:

docker run virtuslab/scala-cli:latest about
HelloWorld.scala
object HelloWorld extends App {
println("Hello world")
}
docker run  -v $(pwd)/HelloWorld.scala:/HelloWorld.scala virtuslab/scala-cli /HelloWorld.scala
# Hello world