VOOZH about

URL: https://www.geeksforgeeks.org/scala/command-line-argument-in-scala/

⇱ Command Line Argument in Scala - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Command Line Argument in Scala

Last Updated : 26 May, 2021

The arguments which are passed by the user or programmer to the main() method are termed as Command-Line Arguments. main() method is the entry point of execution of a program. main() method accepts an array of strings. 
runtime. But it never accepts parameters from any other method in the program. 
Syntax: 
 

def main(args: Array[String])


For accessing our Scala command-line arguments using the args array, which is made available to us implicitly when we extend App. Here is an example.
Example 1: Print all given objects 
 

To Compile and execute the above program on terminal follow below commands : 
First save program CMDExample.scala then open CMD/Terminal and go on that directory where you save your scala program.
 

Compile: scalac CMDExample.scala 
Execute: scala CMDExample Welcome To GeeksforGeeks! 
 


Output: 
 

Scala Command Line Argument Example
Welcome
To
GeeksforGeeks!


 

👁 Image


Example 2: Print some object which is given at runtime 
 

To Compile and execute the above program on terminal follow below commands : 
 

Compile: scalac CMDExample.scala 
Execute: scala CMDExample 1 Welcome To GeeksforGeeks! 2 
 


Output: 
 

Scala Command Line Argument Example
1
To


 

👁 Image


Note:If given index not present in array then you find this error 
 

👁 Image


 

Comment
Article Tags:

Explore