âšī¸ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.1 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.geeksforgeeks.org/java/command-line-arguments-in-java/ |
| Last Crawled | 2026-04-10 00:10:55 (3 days ago) |
| First Indexed | 2025-06-13 01:16:41 (10 months ago) |
| HTTP Status Code | 200 |
| Meta Title | Command Line Arguments in Java - GeeksforGeeks |
| Meta Description | Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more., Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions. |
| Meta Canonical | null |
| Boilerpipe Text | Last Updated :
24 Mar, 2026
Java command-line argument
Â
is an argument, i.e., passed at the time of running the Java program. Command-line arguments passed from the console can be received by the Java program and used as input.
Example:
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 Program to Illustrate First Argument
class
GFG
{
public
static
void
main
(
String
[]
args
)
{
// Printing the first argument
System
.
out
.
println
(
args
[
0
]
);
}
}
Output:
Output of first argument
Explanation
:
Running java GFG GeeksForGeeks prints GeeksForGeeks because the argument is passed to main(String[] args).
If no arguments are given (e.g., java GFG), it throws ArrayIndexOutOfBoundsException since args 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
Command-line arguments in Java are space-separated values passed to the main(String[] args) method.
JVM wraps them into the args[] array, where each value is stored as a string (e.g., args[0], args[1], etc.).
The number of arguments can be checked using args.length.
Example
: Display Command-Line Arguments Passed to a Java 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.
class
Hello
{
// 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:
Command Line Arguments in Java
Visit Course
Suggested Quiz
3 Questions
Why are command-line arguments useful in Java?
A
They speed up compilation
B
They remove the need for a main method
C
They allow providing input at runtime
D
They convert programs into scripts
How can you check the number of command-line arguments passed to a Java program?
A
args.size()
B
args.count()
C
args.length
D
args.getLength()
Where does the JVM store command-line arguments before passing them to the program?
A
In a HashMap
B
In an integer array
C
In the args[] array of Strings
D
In a file
Quiz Completed Successfully
Your Score :
0
/
3
Accuracy :
0%
Login to View Explanation
1
/3
Comment
Article Tags:
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java |
| Markdown | [](https://www.geeksforgeeks.org/)

- Sign In
- [Courses]()
- [Tutorials]()
- [Interview Prep]()
- [Java Tutorial](https://www.geeksforgeeks.org/java/java/)
- [Advanced Java](https://www.geeksforgeeks.org/advance-java/advanced-java/)
- [Interview Questions](https://www.geeksforgeeks.org/java/java-interview-questions/)
- [Exercises](https://www.geeksforgeeks.org/java/java-exercises/)
- [Examples](https://www.geeksforgeeks.org/java/java-programming-examples/)
- [Quizzes](https://www.geeksforgeeks.org/java/java-quiz/)
- [Projects](https://www.geeksforgeeks.org/blogs/java-projects/)
- [Cheatsheet](https://www.geeksforgeeks.org/java/java-cheat-sheet/)
- [DSA in Java](https://www.geeksforgeeks.org/dsa/dsa-in-java/)
- [Java Collection](https://www.geeksforgeeks.org/java/java-collection-tutorial/)
# Command Line Arguments in Java
Last Updated : 24 Mar, 2026
Java command-line argumentis an argument, i.e., passed at the time of running the Java program. Command-line arguments passed from the console can be received by the Java program and used as input.
Example:
> 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
``
****Output:****

Output of first argument
****Explanation****:
- Running java GFG GeeksForGeeks prints GeeksForGeeks because the argument is passed to main(String\[\] args).
- If no arguments are given (e.g., java GFG), it throws ArrayIndexOutOfBoundsException since args 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
- Command-line arguments in Java are space-separated values passed to the main(String\[\] args) method.
- JVM wraps them into the args\[\] array, where each value is stored as a string (e.g., args\[0\], args\[1\], etc.).
- The number of arguments can be checked using args.length.
### ****Example****: Display Command-Line Arguments Passed to a Java 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.
Java
``
****Output:****

Command Line Arguments in Java
[Visit Course](https://www.geeksforgeeks.org/courses/java-online-course-complete-beginner-to-advanced)
Suggested Quiz

3 Questions
Why are command-line arguments useful in Java?
- A
They speed up compilation
- B
They remove the need for a main method
- C
They allow providing input at runtime
- D
They convert programs into scripts
How can you check the number of command-line arguments passed to a Java program?
- A
args.size()
- B
args.count()
- C
args.length
- D
args.getLength()
Where does the JVM store command-line arguments before passing them to the program?
- A
In a HashMap
- B
In an integer array
- C
In the args\[\] array of Strings
- D
In a file

Quiz Completed Successfully
Your Score :0/3
Accuracy :0%
Login to View Explanation
**1**/3
\< Previous
Next \>
Comment
[T](https://www.geeksforgeeks.org/user/Twinkle%20Tyagi/)
twinkle tyagi
193
Article Tags:
Article Tags:
[Java](https://www.geeksforgeeks.org/category/programming-language/java/)
[java-basics](https://www.geeksforgeeks.org/tag/java-basics/)
### Explore
[](https://www.geeksforgeeks.org/)

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
[](https://geeksforgeeksapp.page.link/gfg-app)[](https://geeksforgeeksapp.page.link/gfg-app)
- Company
- [About Us](https://www.geeksforgeeks.org/about/)
- [Legal](https://www.geeksforgeeks.org/legal/)
- [Privacy Policy](https://www.geeksforgeeks.org/legal/privacy-policy/)
- [Contact Us](https://www.geeksforgeeks.org/about/contact-us/)
- [Advertise with us](https://www.geeksforgeeks.org/advertise-with-us/)
- [GFG Corporate Solution](https://www.geeksforgeeks.org/gfg-corporate-solution/)
- [Campus Training Program](https://www.geeksforgeeks.org/campus-training-program/)
- Explore
- [POTD](https://www.geeksforgeeks.org/problem-of-the-day)
- [Job-A-Thon](https://practice.geeksforgeeks.org/events/rec/job-a-thon/)
- [Blogs](https://www.geeksforgeeks.org/category/blogs/?type=recent)
- [Nation Skill Up](https://www.geeksforgeeks.org/nation-skill-up/)
- Tutorials
- [Programming Languages](https://www.geeksforgeeks.org/computer-science-fundamentals/programming-language-tutorials/)
- [DSA](https://www.geeksforgeeks.org/dsa/dsa-tutorial-learn-data-structures-and-algorithms/)
- [Web Technology](https://www.geeksforgeeks.org/web-tech/web-technology/)
- [AI, ML & Data Science](https://www.geeksforgeeks.org/machine-learning/ai-ml-and-data-science-tutorial-learn-ai-ml-and-data-science/)
- [DevOps](https://www.geeksforgeeks.org/devops/devops-tutorial/)
- [CS Core Subjects](https://www.geeksforgeeks.org/gate/gate-exam-tutorial/)
- [Interview Preparation](https://www.geeksforgeeks.org/aptitude/interview-corner/)
- [Software and Tools](https://www.geeksforgeeks.org/websites-apps/software-and-tools-a-to-z-list/)
- Courses
- [ML and Data Science](https://www.geeksforgeeks.org/courses/category/machine-learning-data-science)
- [DSA and Placements](https://www.geeksforgeeks.org/courses/category/dsa-placements)
- [Web Development](https://www.geeksforgeeks.org/courses/category/development-testing)
- [Programming Languages](https://www.geeksforgeeks.org/courses/category/programming-languages)
- [DevOps & Cloud](https://www.geeksforgeeks.org/courses/category/cloud-devops)
- [GATE](https://www.geeksforgeeks.org/courses/category/gate)
- [Trending Technologies](https://www.geeksforgeeks.org/courses/category/trending-technologies/)
- Videos
- [DSA](https://www.geeksforgeeks.org/videos/category/sde-sheet/)
- [Python](https://www.geeksforgeeks.org/videos/category/python/)
- [Java](https://www.geeksforgeeks.org/videos/category/java-w6y5f4/)
- [C++](https://www.geeksforgeeks.org/videos/category/c/)
- [Web Development](https://www.geeksforgeeks.org/videos/category/web-development/)
- [Data Science](https://www.geeksforgeeks.org/videos/category/data-science/)
- [CS Subjects](https://www.geeksforgeeks.org/videos/category/cs-subjects/)
- Preparation Corner
- [Interview Corner](https://www.geeksforgeeks.org/interview-prep/interview-corner/)
- [Aptitude](https://www.geeksforgeeks.org/aptitude/aptitude-questions-and-answers/)
- [Puzzles](https://www.geeksforgeeks.org/aptitude/puzzles/)
- [GfG 160](https://www.geeksforgeeks.org/courses/gfg-160-series)
- [System Design](https://www.geeksforgeeks.org/system-design/system-design-tutorial/)
[@GeeksforGeeks, Sanchhaya Education Private Limited](https://www.geeksforgeeks.org/), [All rights reserved](https://www.geeksforgeeks.org/copyright-information/)
![]() |
| Readable Markdown | Last Updated : 24 Mar, 2026
Java command-line argumentis an argument, i.e., passed at the time of running the Java program. Command-line arguments passed from the console can be received by the Java program and used as input.
Example:
> 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.
``
****Output:****

Output of first argument
****Explanation****:
- Running java GFG GeeksForGeeks prints GeeksForGeeks because the argument is passed to main(String\[\] args).
- If no arguments are given (e.g., java GFG), it throws ArrayIndexOutOfBoundsException since args 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
- Command-line arguments in Java are space-separated values passed to the main(String\[\] args) method.
- JVM wraps them into the args\[\] array, where each value is stored as a string (e.g., args\[0\], args\[1\], etc.).
- The number of arguments can be checked using args.length.
### ****Example****: Display Command-Line Arguments Passed to a Java 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.
``
****Output:****

Command Line Arguments in Java
[Visit Course](https://www.geeksforgeeks.org/courses/java-online-course-complete-beginner-to-advanced)
Suggested Quiz
3 Questions
Why are command-line arguments useful in Java?
- A
They speed up compilation
- B
They remove the need for a main method
- C
They allow providing input at runtime
- D
They convert programs into scripts
How can you check the number of command-line arguments passed to a Java program?
- A
args.size()
- B
args.count()
- C
args.length
- D
args.getLength()
Where does the JVM store command-line arguments before passing them to the program?
- A
In a HashMap
- B
In an integer array
- C
In the args\[\] array of Strings
- D
In a file

Quiz Completed Successfully
Your Score :0/3
Accuracy :0%
Login to View Explanation
**1**/3
Comment
Article Tags:
### Explore |
| Shard | 103 (laksa) |
| Root Hash | 12046344915360636903 |
| Unparsed URL | org,geeksforgeeks!www,/java/command-line-arguments-in-java/ s443 |