Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Friday 2 June 2017

Command Line Arguments In Java

 Command Line Arguments

Command Line Arguments In Java

Java command line arguments is an arguments which is passed when we run or executes our java program after compilation.

Java command line arguments are passed from the console or command prompt when we run our java programs

When we passed the arguments from the console or command prompt then it can be received in the java program and it can be used as an input.

In java by default all the command line arguments will be treated as String value and these String values are stored in a String array of the main() method of java programs.

Syntax of Command Line Arguments

Suppose, We edited a simple java program and then we will compile it and run it.

compile by : javac Test.java
run by : java Test value1 value2 value3 value4 ....

Here value1 value2 value3 value4 is a command line arguments


Java Command Line Arguments Example

This is simple example of command line arguments  where we will pass arguments from the console at the time of running java program.

class CLAExample
{
public static void main(String args[])
{
System.out.println("Argument from cmd " +args[0]);
}
}

compile : javac CLAExample.java
run : java CLAExample  javatutorial

output : Argument from cmd javatutorial


Java Command Line Arguments Example 2

Now this is another example of java command line arguments where we will print all the arguments that we passed from the command line and for traverse all the values from the array we use loop.

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

now compile it and run it and give arguments

compile : javac CLAExample2.java
run : java CLAExample2 java 1 2 3 programming

output : java
              1
              2
              3
              programming



Java Command Line Arguments Example 3

In this example we will print all the arguments and length of the arguments we passed from the command line.

class CLAExample3
{
public static void main(String s[])
{
System.out.println("Length of the arguments "+s.length);
for(int i = 0; i<s.length; i++)
{
System.out.println(s[i]);
}
}
}

now give the arguments at run time

compile : javac CLAExample3.java
run : java CLAExample3 red green blue

output : Length of the arguments 3
              red
              green
              blue


Java Command Line Arguments Example 4

In this example, We will not give any arguments from the command line or console. If we will not give any arguments from the command line it will throw compile time error.

class CLAExample4
{
public static void main(String args[])
{
System.out.println(args[0]);
}
}

compile : javac CLAExample4.java
run : java CLAExample4

output :  Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException


Some Important Points About Command Line Arguments

There are some points about command line arg.
  • In java all the commands line arguments would be accepted by the JVM.
  • In java, Command line arguments accepted in the form of String object by the JVM.
  • JVM always executes main method by passing objects of an array of String.
  • JVM never execute main method in java by passing null values.

Read More:


Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate