Command line arguments

Command-line arguments:

In this blog we will learn command line arguments in java and also learn how to pass command line arguments in java.

Command line arguments

The arguments which are passing from command prompt are called command line arguments.

The main objective of command line arguments are we can customize the behavior of the main () method.

If any input value is passed through command prompt at the time of running of program is known as command line argument by default every command line argument will be treated as string value and those are stored in string array of main() method.

Syntax for Compile and Run CMD programs

Compile By  — ->      Javac  Mainclass.java

Run By    —–>            Java  Mainclass value1 value2  value3 ………………..

Example of command-line argument in java

class CommandLineExample
{
public static void main(String args[])
{
System.out.println(“Argument is: “+args[0]);
}
}

Compile and Run above programs

Compile By > Javac  CommandLineExample.java

Run By > Java  CommandLineExample  Porter

Output

Argument is: Porter

When the above statement is executing the following sequence of steps will take place.

  • Class loader sub-system loads SumDemo along with Command line argument (10, 20) and in main memory.
  • JVM take the loaded class SumDemo along with Command line arguments (10, 20) and place the number of values in the length variable that is 2.
  • JVM looks for main() that is JVM will place the Command in the main() in the form of string class that is.
  • Hence all the CMD line arguments of java are sending to main() method available in the form of array of object of String class (every CMD are available or stored in main method in the form of array of object of String class).
  • JVM calls the main() method with respect to loaded class SumDemo that is SumDemo.main().

Accept command line arguments and display their values
class CMD
{
public static void main(String k[])
{
System.out.println(“no. of arguments =”+k.length);
for(int i=0;i< k.length;i++)
{
System.out.println(k[i]);
}
}
}

Square of Number by reading value from command prompt.

class squareDemo
{
int no, result;
void square(String s)
{
int no=Integer.parseInt(s);
result=no*no;
System.out.println(“Square: ” +result);
}
}
class CMD
{
public static void main(String args[])
{
System.out.println(“no of arguments: “+args.length);
squareDemo obj=new squareDemo();
obj.square(args[0]);
}
}


Note:1-
class TestCMD
{
public static void main(String args[])
{
for(int i=0;i<= args.length;i++)
{
System.out.println(args[i]);
}
}
}
OUTPUT:

• Java TestCMD A B C
A
B
C
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 3
at TestCMD.main(CMD.java:8)

• Java TestCMD A B
A
B
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 2
at TestCMD.main(CMD.java:8)

• Java TestCMD
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
at TestCMD.main(CMD.java:8)

 Solution:-if we replace <= with < then we wont get any RunTime Exception

Note:2-
class TestCMD
{
public static void main(String args[])
A B C
{
String[] argh={“x”,”y”,”z”};
args=argh;
x y z

for(String s:args)
{
System.out.println(s);
}
}
}
OUTPUT:

• Java TestCMD A B C
x
y
z

• Java TestCMD A B
x
y
z

• Java TestCMD
x
y
z

Note:3-
class TestCMD
{
public static void main(String args[])
{
System.out.println(args[0]+args[1]);
}
}

OUTPUT:

• Java TestCMD 10 20
1020
Note: Except + operator any numeric operation not allowed in command line arguments.
Note:4-
class TestCMD
{
public static void main(String args[])
{
System.out.println(args[0]);
}
}

OUTPUT:

• Java TestCMD “Hello java”

     Hello java”

Leave a Comment

Your email address will not be published. Required fields are marked *