Movatterモバイル変換


[0]ホーム

URL:


Open In App

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
// Java Program to Illustrate First ArgumentclassGFG{publicstaticvoidmain(String[]args){// Printing the first argumentSystem.out.println(args[0]);}}

Output:

Output 1
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
classGeeks{// Main driver methodpublicstaticvoidmain(String[]args){// Checking if length of args array is// greater than 0if(args.length>0){// Print statementsSystem.out.println("The command line"+" arguments are:");// Iterating the args array// using for each loopfor(Stringval:args)System.out.println(val);}elseSystem.out.println("No command line "+"arguments found.");}}

Output:

Output 2


Command Line Arguments in Java
Visit Courseexplore course icon

T

twinkle tyagi
Improve

T

twinkle tyagi
Improve
Article Tags :

Explore

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

[8]ページ先頭

©2009-2025 Movatter.jp