0% found this document useful (0 votes)
1K views

Simple Example of Command-Line Argument in Java

Java command line arguments allow values to be passed into a Java program when it is run. These arguments can be accessed via the args parameter in the main method. Programs can be tested with different input values by passing arguments from the command line when running the program. Arguments are represented as strings in the args array, and programs can print or use the argument values.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Simple Example of Command-Line Argument in Java

Java command line arguments allow values to be passed into a Java program when it is run. These arguments can be accessed via the args parameter in the main method. Programs can be tested with different input values by passing arguments from the command line when running the program. Arguments are represented as strings in the args array, and programs can print or use the argument values.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Command Line Arguments

The java command-line argument is an argument i.e. passed at the time of running the
java program.
The arguments passed from the console can be received in the java program and it can
be used as an input.
So, it provides a convenient way to check the behavior of the program for the different
values. You can pass N (1,2,3 and so on) numbers of arguments from the command
prompt.

Simple example of command-line argument in java


In this example, we are receiving only one argument and printing it. To run this java
1.
2.
3.
4.
5.
1.
2.

program, you must pass at least one argument from the command prompt.
class CommandLineExample{
public static void main(String args[]){
System.out.println("Your first argument is: "+args[0]);
}
}
compile by > javac CommandLineExample.java
run by > java CommandLineExample sonoo
Output: Your first argument is: sonoo

Example of command-line argument that prints all the


values
In this example, we are printing all the arguments passed from the command-line.
1.
2.
3.
4.
5.
6.
7.
8.
1.
2.

For this purpose, we have traversed the array using for loop.
class A{
public static void main(String args[]){
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}
}
compile by > javac A.java
run by > java A sonoo jaiswal 1 3 abc

Output: sonoo
jaiswal
1
3
abc

You might also like