Monday, April 29, 2013

How To Use Command Line Arguments in Eclipse ?

A Java application allows users to enter command line arguments at runtime, and also users can enter N number of arguments using command line.
Command Line Arguments
Here in this post we have created a simple Arithmetic Application to add 2 numbers that get passed as Command Line Arguments.
1. Create a Java Application AdditionJar.
image
2. Create a class named Addition inside com.arithmetic package.
3. Get the two command line arguments and convert it into Integer like below.
package com.arithmetic;
public class Addition {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int number1=Integer.parseInt(args[0]);
        int number2=Integer.parseInt(args[1]);
        System.out.println("Addition of given two numbers:"+(number1+number2));
    }
} 

4. Go to project properties –> Run/Debug Settings –> Click Addition –> Edit
image

5. Once done with click of edit button , goto arguments –> Enter the first and second argument with a space.
image
6. Output window displays the addition of two command line arguments.
image

No comments:

Post a Comment