Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Command Line Arguments in Java
Next article icon

Command Line Arguments in Java

Last Updated : 22 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Java command-line argument is an argument, i.e., passed at the time of running the Java program. In Java, the command line arguments passed from the console can be received in the Java program, and they can be used as input. The users can pass the arguments during the execution, bypassing the command-line arguments inside the main() method.

What are Command Line Arguments?

When we run a Java program, we can give extra information right after the program name. This extra information is called command-line arguments. Let's understand this with the help of an example. If we run this,

java Geeks Hello World

Note: Here, the words Hello and World are the command line arguments. JVM will collect these words and will pass these arguments to the main method as an array of strings called args. The JVM passes these arguments to the program inside args[0] and args[1].

Example: In this example, we are going to print a simple argument in the command line.

Java
// Java Program to Illustrate First Argument class GFG{      public static void main(String[] args) {                // Printing the first argument         System.out.println(args[0]);     } } 

Output:

Output 1

Explanation: If we run a Java Program by writing the command "java GFG GeeksForGeeks" where the name of the class is "GFG", then it will print GeeksforGeeks. If no arguments are provided (e.g., just java GFG), it will throw an ArrayIndexOutOfBoundsException because the args array is empty.

Why Use Command Line Arguments?

  • It is used because it allows us to provide input at runtime without modifying the whole program.
  • It helps to run programs automatically by giving them the needed information from outside.

Working of Command-Line Arguments

We need to pass the arguments as space-separated values. We can pass both strings and primitive data types(int, double, float, char, etc) as command-line arguments. These arguments convert into a string array and are provided to the main() function as a string array argument.

When command-line arguments are supplied to JVM, JVM wraps these and supplies them to args[]. It can be confirmed that they are wrapped up in an args array by checking the length of args using args.length.

Internally, JVM wraps up these command-line arguments into the args[ ] array that we pass into the main() function. We can check these arguments using args.length method. JVM stores the first command-line argument at args[0], the second at args[1], the third at args[2], and so on.

Example: Display Command-Line Arguments Passed to a Java Program

Java
// Java Program to Check for Command Line Arguments class Geeks {      // Main driver method     public static void main(String[] args)     {         // Checking if length of args array is         // greater than 0         if (args.length > 0) {              // Print statements             System.out.println("The command line"                                + " arguments are:");              // Iterating the args array             // using for each loop             for (String val : args)                                 System.out.println(val);         }         else                         System.out.println("No command line "                                + "arguments found.");     } } 

Output:

Output 2

Steps to Run the Above Program

To compile and run a Java program in the command prompt, follow the steps written below.

  • Save the program as Hello.java
  • Open the command prompt window and compile the program- javac Hello.java
  • After a successful compilation of the program, run the following command by writing the arguments- java Hello
  • For example - java Hello Geeks at GeeksforGeeks
  • Press Enter and you will get the desired output.

Important Points:

  • String Input: It takes input in the space separated format.
  • Dynamic-input: Allows user to pass dynamic inputs.
  • Length: We can check the length using .length property.
  • Indexes: We can take the values just using the indexes args[0] args[1]......

Next Article
Command Line Arguments in Java

T

Twinkle Tyagi
Improve
Article Tags :
  • Java
  • java-basics
Practice Tags :
  • Java

Similar Reads

    Argument vs Parameter in Java
    Argument An argument is a value passed to a function when the function is called. Whenever any function is called during the execution of the program there are some values passed with the function. These values are called arguments. An argument when passed with a function replaces with those variabl
    2 min read
    Java -verbose Command
    Java Development Kit (JDK) is the Software Development Kit that contains all the tools and files required for compiling, developing, and running Java programs. JDK has the Java Compiler Program known as Javac which compiles the Java source files to the bytecode class files i.e. ( .java to .class). J
    4 min read
    Rules For Variable Declaration in Java
    Variable in Java is a data container that saves the data values during Java program execution. Every variable is assigned a data type that designates the type and quantity of value it can hold. Variable is a memory location name of the data. A variable is a name given to a memory location. For More
    2 min read
    Java Program to Open the Command Prompt and Insert Commands
    In Java, the Runtime class allows Java applications to interact with the system's environment. Every Java application has an object of this class, this class provides exec() method and this method is used to run system commands.In this article, we will discuss how the exec() method can be used to op
    2 min read
    File length() method in Java with Examples
    The length() function is a part of File class in Java . This function returns the length of the file denoted by the this abstract pathname was length.The function returns long value which represents the number of bytes else returns 0L if the file does not exists or if an exception occurs. Function s
    2 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