Command Line Argument in Java

Command Line Argument

The java command-line argument is an argument i.e. passed at the
time of running the java program.
The command-line arguments are passed to the program at run-time. 
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 behaviour of the program for the different values. You can pass N (1,2,3 and so on) numbers of arguments from the command prompt.
Command Line Arguments can be used to specify configuration information while launching your application.
There is no restriction on the number of java command line arguments.
You can specify any number of arguments Information is passed as Strings.They are captured into the String args of your main method.
The first command-line argument
is stored at args[0], the second at args[1], and so on. 


Example While running a class cmd, you can specify command line arguments as

$java cmd arg1 arg2 arg3 …
you must pass at least one argument from the command prompt.


Example 1 :
public class CommandLine
{
public static void main(String args[])
{
System.out.println("Your first argument is: "+args[0]);
}
}

compile by > javac CommandLine.java
run by > java CommandLine Omkar


Output:

Your first argument is:omkar


Example 2:
Write a program to accept number from command line and print square root of the number.


program:
public class CommandLine
{
public static void main(String args[])
{
int num;
num= Integer.parseInt(args[0]);
double sq=Math.sqrt(num);
System.out.println("square root of "+ num +" is" :+sq);
}
}


compile by > javac CommandLine.java
run by > java CommandLine 25


Output:
square root of 25 is :5.0


Example 3:
Program to accept two numbers as command line arguments and print the addition of two numbers.


Program:
public class commandLine
{
public static void main(String args[])
{
int num1 = Integer.parseInt(args[0]);
int num2 = Integer.parseInt(args[1]);
int Addition = num1 + num2;
System.out.println("Addition of number is : "+Addition);
}
}


compile by > javac CommandLine.java
run by > java CommandLine 5 10


Output:
Addition of number is :20


Example 4:
Write a java program to find factorial of a number using command line argument.


program:
public class CommandLine
{
public static void main(String args[])
{
int n;
int f=1;
n= Integer.parseInt(args[0]);
for(int i=1;i<=n;i++)
{
f=f*i;
}
System.out.println("factorial  of "+ n +" is :"+f);
}
}


compile by > javac CommandLine.java
run by > java CommandLine  5


Output:
factorial of 5 is: 120


Example 5:
Write a program to print reverse of a number using command line argument.


program:
class CommandLine
{
public static void main(String args[])
{
int num = Integer.parseInt(args[0]);
int remainder, result=0;
while(num>0)
{
remainder = num%10;
result = result * 10 + remainder;
num = num/10;
}
System.out.println("Reverse number is : "+result);
}
}


compile by > javac CommandLine.java
run by > java CommandLine  123


Output:
Reverse number is:321


Example 6:
Write a program to compute the sum of the digits of a given integer numbers.


program:
class CommandLine
{
public static void main(String args[])
{
int num = Integer.parseInt(args[0]); //takes argument
as commandline
int remainder, result=0;
while(num>0)
{
remainder = num%10;
result = result + remainder;
num = num/10;
}
System.out.println("Sum of digit of number is : "+result);
}
}


compile by > javac CommandLine.java
run by > java CommandLine  123


Output:
Sum of digit of number is: 6


Example 7:
Program to input names from command line arguments.


Program:
public class Hello
{
public static void main(String args[])
{
for(int i=0;i<args.length;i++)
 System.out.println(“Hello ” + args[i]);
 }
}


Compile by >javac Hello.java
Run by> java Hello manju om muttu


Output:
Hello manju
Hello om
Hello muttu


Example 8:
Write a java program to sort an array input from command line.


program:
public class SortCmd
{
public static void main(String args[])
{
int n=args.length;
int a[ ]=new int[n];

for(int i=0;i<n;i++)
a[i] = Integer.parseInt(args[i]);

//sorting
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if(a[i]>a[j])
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
System.out.println("sorted array is:");
for(int i=0;i<n;i++)
System.out.print(a[i]+”\t”);
}
}


Compile by: javac SortCmd.java
Run by: java SortCmd 5 4 1 3 2


Output:
sorted array is:
1 2 3 4 5


Question:
Explain the command line arguments with suitable example.

Comments

Popular posts from this blog

Control Statements:Selection statement ,Iteration statement and Jump statement in Java

Applets - Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet, passing parameters to applets, applet security issues.

Java Database Connectivity (JDBC) ,Steps to connect to the database in Java