• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Passing arguments and properties from command line

October 9, 2007 //  by Krishna Srinivasan//  Leave a Comment

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:

  • Java Tutorials
  • Java EE Tutorials
  • Design Patterns Tutorials
  • Java File IO Tutorials

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

Category: JavaTag: Core Java

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.

Previous Post: « Generation of Random Numbers
Next Post: How to send mail using Java Mail API? »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact