Passing arguments and properties from command line

Arguments and properties can be passed to a java application from command line.
In this techical tip, let us see how to pass arguments as well as properties from command line.

also read:

Passing arguments from command line

The syntax to pass arguments from command line is as follows,

java <classname>  <argument1>  <argument2>  <argument3> ........

The following example shows the usage of command line arguments.

ArithmeticOperationTest.java


public class ArithmeticOperationTest {

    public static void main(String[] arguments) {

        if(arguments.length > 0)
        {
            float numberOne = getNumber(arguments[0]);
            float numberTwo = getNumber(arguments[1]);
            char operator = getOperator(arguments[2]);
            float result = 0.00f;

            if (operator == '+'){
                result = numberOne + numberTwo;
            }else if (operator == '-'){
                result = numberOne - numberTwo;
            }else if (operator == '/'){
                result = numberOne / numberTwo;
            }

            System.out.println("result of " +  numberOne + " " + operator
                + " " + numberTwo + " is " + result);
        }
    }

    static float getNumber(String number){

        float retNumber = 0.0f;
        retNumber = Float.parseFloat(number.trim());
        return retNumber;
    }

    static char getOperator(String argument){
        char retOperator = ' ';
        retOperator = argument.trim().charAt(0);
        return retOperator;
    }
}

To run the above program issue the following at command line,

java ArithmeticOperationTest 10 5 /

The output is,

result of 10.0 / 5.0 is 2.0

Now, let us see how to pass properties from command line.

Passing properties from command line

The syntax to pass properties from command line is as follows,

java -D<property1-name>=<property1-value> -D<property2-name>=<property2-value> ....... <class-Name>

The getProperty() method in System class is used to get the value of a property by specifying the property name as key.

Consider the following example,

public class ScoreCardPropertiesTest {

    public static void main(String[] args) {

        String subject = System.getProperty("subject");
        System.out.println("subject is " +subject);
        String score = System.getProperty("score");
        System.out.println("score is " +score);
        String grade = System.getProperty("grade");
        System.out.println("grade is " +grade);
    }
}

We pass properties subject, score and grade to the above ScoreCardPropertiesTest class as follows,

java -Dsubject=Maths -Dscore=95 -Dgrade=A ScoreCardPropertiesTest

The output is,

subject is Maths
score is 95
grade is A

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*