How to Use Command Line Arguments in Scala

Advertisement

Advertisement

Scala programs can accept run-time variables as command line arguments. Here is an example of how to access the variables.

object CmdArgs {
    def main(args: Array[String]) {

        println("All Command Line Arguments: " + args.toList)
        println("First argument: " + args(0)) // First argument

    }
}

Compile it, and run it with some command line arguments. If you don't provide at least one argument it will fail due to out of bounds index.

scalac CmdArgs.scala
scala CmdArgs First Second

Advertisement

Advertisement