Download Java Command Line Arguments

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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
The command line argument is the argument passed to a
program at the time when you run it. To access the command-line
argument inside a java program is quite easy, they are stored as
string in String array passed to the args parameter
of main() method.
Simple example1 of command-line argument in java
In this example, we are receiving only one argument and
printing it. To run this java program, you must pass at least one
argument from the command prompt.
1. class CommandLineExample{
2. public static void main(String args[]){
3. System.out.println("Your first argument is: "+args[0]);
4. } }
Compile by > javac CommandLineExample.java
Run by > java CommandLineExample sonoo
Output: Your first argument is: sonoo
Example2 of command-line argument that prints all the
values
In this example, we are printing all the arguments passed from
the command-line. For this purpose, we have traversed the
array using for loop.
1. class A{
2. public static void main(String args[]){
3.
4. for(int i=0;i<args.length;i++)
5. System.out.println(args[i]);
6. }
7. }
Compile by > javac A.java
Run by > java A Kate How 1 3 abc
Output: Kate
How
1
3
abc
Example 3:
class cmd
{
public static void main(String[] args)
{
for(int i=0;i< args.length;i++)
{
System.out.println(args[i]);
}
}
}
Compile:>javac cmd
Execute > java cmd 10 20 30
Output :
10
20
30