Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
Getting Started with Scala and sbt on the Command Line
Next article icon

Command Line Argument in Scala

Last Updated : 26 May, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 
 

Scala
// Scala Program on command line argument object CMDExample {     // Main method     def main(args: Array[String])      {         println("Scala Command Line Argument Example");                  // You pass any thing at runtime          // that will be print on the console         for(arg<-args)         {             println(arg);         }     } } 

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!


 


Example 2: Print some object which is given at runtime 
 

Scala
// Scala Program on command line argument object CMDExample  {     // Main method     def main(args: Array[String])     {         println("Scala Command Line Argument Example");                  // You pass any thing at runtime          // that will be print on the console         println(args(0));         println(args(2));     } } 

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


 


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


 


Next Article
Getting Started with Scala and sbt on the Command Line
author
29AjayKumar
Improve
Article Tags :
  • Scala
  • Scala
  • Scala-Basics

Similar Reads

  • Scala | Named Arguments
    In Scala when arguments passes through the function with a named parameters, we can label the arguments with their parameter names.These named arguments are cross matched with the named parameters of function. In normal scenario Unnamed Parameters uses Parameter Positions to make Function or Constru
    3 min read
  • Comments In Scala
    Comments are entities, in our code, that the interpreter/compiler ignores. We generally use them to explain the code, and also to hide code details. It means comments will not be the part of the code. It will not be executed, rather it will be used only to explain the code in detail.In other words,
    3 min read
  • How to access list elements in Scala?
    In this articleIn this article, we will learn to access list elements in Scala. List in Scala is the most commonly used collection type, providing an ordered collection that allows accessing elements by index or using functional programming. How to Access List Elements in Scala?Below are the possibl
    3 min read
  • Scala Programming Language
    Scala is a general-purpose, high-level, multi-paradigm programming language. It is a pure object-oriented programming language which also provides support to the functional programming approach. Scala programs can convert to bytecodes and can run on the JVM (Java Virtual Machine). Scala stands for S
    3 min read
  • Getting Started with Scala and sbt on the Command Line
    Scala is a potent general-purpose programming language with structured query data types that supports both operational and object-oriented programming. Many of Scala's design decisions are intended to be brief and try to address Java's critics. In this article learn how to create a Scala project fro
    2 min read
  • Scala Console | println, printf and readLine
    Console implements functions for displaying the stated values on the terminal i.e, with print, println, and printf we can post to the display. It is also utilized in reading values from the Console with the function from scala.io.StdIn. It is even helpful in constructing interactive programs. Let's
    2 min read
  • Interesting fact about Scala
    Scala(pronounced as "skah-lah") is general-purpose programming language designed by Martin Odersky. The design of Scala started in 2001 at EPFL, Lausanne, Switzerland. Scala was released publicly in 2004 on Java platform. Scala is designed to be concise and addresses criticisms of Java. Scala source
    3 min read
  • Scala Long >=(x: Int) method
    In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The >=(x: Int) method is utilized to return true if this value is greater than or equal to x, false otherwise. Method Definition - def >=(x: Int): Boolean Returns - Returns true if this value is great
    1 min read
  • Scala Long >(x: Int) method
    In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The >(x: Int) method is utilized to return true if this value is greater than x, false otherwise. Method Definition - def >(x: Int): Boolean Returns - Returns true if this value is greater than x, fal
    1 min read
  • Lambda Expression in Scala
    Lambda Expression refers to an expression that uses an anonymous function instead of variable or value. Lambda expressions are more convenient when we have a simple function to be used in one place. These expressions are faster and more expressive than defining a whole function. We can make our lamb
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences