Run
The run
command runs your Scala code:
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 --
:
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:
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:
#!/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
object HelloWorld extends App {
println("Hello world")
}
docker run -v $(pwd)/HelloWorld.scala:/HelloWorld.scala virtuslab/scala-cli /HelloWorld.scala
# Hello world