Java Command Line Arguments example. How to pass program arguments in Eclipse

Command Line Arguments:

The arguments which we pass on to main( ) at the command prompt are called command line arguments. A Java Program can accept any number of arguments from the command line. The function main( ) can have one argument which is array of Strings ie. main(String args[]). Command line arguments are specified after the name of the class. Arguments are separated by space. When the program is executed, the strings at the command line are passed to the string arrays.  Argument with space should be passed with in quotes so that it can be considered as single argument.

Syntax : java classname arg1 arg2  arg3  ……

Example : Java filecopy Test1.txt Test2.txt

where test1.txt is passed to args[0] , test2.txt to args[1].

Now the number of arguments will be the length of the array which is args.length field

How to Display Command-Line Arguments:

The Test example prints the number of arguments and each command line arguments

public class Test {
    public static void main (String[] args) {

 System.out.println("Number of arguments : " + args.length) ;
        for (String s: args) {
            System.out.println(s);
        }
    }
}

Running the above program :

1) java Test Welcome to JavaOnlineGuide

Output :

Number of arguments : 3

Welcome
to
JavaOnlineGuide.net

2) java Test “Welcome to JavaOnlineGuide”

Output :

Number of arguments : 1

Welcome to JavaOnlineGuide

Note : Space character separates command-line arguments. If we give the argument by enclosing quotation marks, it will be treated as single argument.

Checking Improper Arguments :

Improper arguments can be checked by args.length field ….as shown in the example below

package com.javaonline;

public class FileCopyCLA {
 public static void main (String[] args) {

 if(args.length!=2) 

  {
  System.out.println("Improper arguments. Syntax : java filecopy sourcefile, destfile ");
  System.exit(0);
  }
        for (String s: args) {
            System.out.println(s);
        }
    }
}

The above program requires 2 arguments. Otherwise the program will exit.

Running the above program

java filecopy test1.txt

Output : Improper arguments. Syntax : java filecopy sourcefile, destfile

Parsing Numeric Command Line Arguments:

Suppose an application may require numeric value which needs to be passed by command-line argument. Command line arguments are passed as string, even we send 100 as an argument. So we must convert a String argument which represents a number, such as “100”, to a numeric value. The following code snippet converts a command-line argument to an int:

 

package com.javaonline;

public class sum  {
	   public static void main (String[] args) {

	 int number1;
	 int number2;
	 if(args.length!=2) 

	  {
	  System.out.println("Improper arguments. Syntax : java sum number1 number2");
	  System.exit(0);
	  }
	 else
	  {

	         try
	         {
	          number1= Integer.parseInt(args[0]);
	          number2= Integer.parseInt(args[1]);
	          int sum=number1+number2;
	          System.out.println("Sum of two numbers " + number1 + ", " + number2 + " is = " + sum);

	          } catch (NumberFormatException e) {
	                 System.err.println("Argument must be an integer");
	                 System.exit(1);
	                 }

	                 }
	}

	}

Running the above program

1) java sum 10 90

Output :

Sum of two numbers 10, 90 is = 100

2) java sum 10 five

output :

Argument must be an integer

 

How to Pass Program  Arguments in Eclipse:

Let us consider the above java program.

Right click on the java program (sum.java)

Select Run As -> Run Configurations…

Java command line arguments1

Select Arguments tab and Enter your arguments then apply.

Java command line arguments2

Now run the program.

 

 

 

 

 

You may also like

Leave a Reply