0% found this document useful (0 votes)
7 views3 pages

00 Command Line Arguments in Java

Uploaded by

shahmeerjamal2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

00 Command Line Arguments in Java

Uploaded by

shahmeerjamal2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1|Page Command Line Arguments and Example Programs|

KFC

Command Line Arguments in Java


The command line argument is the argument passed to a program at the time when you run it.
Generally we supply command line arguments when we run the java program from command
line through Command Prompt. 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. So, First argument is args[0], second is args[1] and so
on.

The arguments passed from the console / command prompt 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.

Example

class CMD
{
public static void main(String[] args)
{
for(int i=0;i< args.length;i++)
{
System.out.println(args[i]);
}
}
}
Execute this program as java CMD 10 20 30

Output :

10
20
30

Command line arguments are stored as strings. If we wish to treat them as numbers then we can use
Double.parseDouble() method or Integer.parseInt() method to convert a string argument into
number.

Example : To convert first argument into integer number we will use:


2|Page Command Line Arguments and Example Programs|
KFC

int n1 = Integer.parseInt(args[0]);

// Example Program :Adding two numbers by command line arguments

public class CA

public static void main(String args[])

int n1,n2,sum;

n1=Integer.parseInt(args[0]);

n2=Integer.parseInt(args[1]);

sum=n1+n2;

System.out.println("Addition of two numbers by Command Line arguments=" + sum);

Save as --> CA.java

Compile as--> javac CA.java

Run as --> java CA 10 20

Output
Addition of two numbers by Command Line arguments=30

Using Command Line Arguments in NetBeans IDE


1. Click on Run Menu
2. Go to Select Project Configuration
3. Click on Customize…
4. Type arguments in Arguments box separated by space like 10 20. As shown in the figure.
5. Press F6 to run the project.
6. Output will be shown as
3|Page Command Line Arguments and Example Programs|
KFC

You might also like