Oct 9, 2015

Textual description of firstImageUrl

Passing program arguments and VM arguments in Eclipse, Net beans and Command line

Sometimes when we execute out Java program/project, we need to pass arguments (program and VM). In this post we discusses how to pass these arguments in command line, Eclipse and Net beans. At the end of this post we will see how to pass VM arguments via pom.xml.

Pass program and VM arguments in eclipse:- 

Below program unit expects two program arguments and VM arguments(addProp and multProp).
/**
 * @author devinline
 *
 */
public class ProgramAndVMParaametersPassing {
 
 /**
  *  program arguments are collected in string array referenced by args.
  *  VM arguments can be accessed by System.getProperty("ARGUMENT_NAME")
  */
 public static void main(String[] args) {
  System.out.println("VM arguments addProp: "+ System.getProperty("addProp") 
     +" and multProp: "+ System.getProperty("multProp"));
  System.out.println("Program arguments are " + args[0] +  " and"+ args[1]);
  if(Boolean.parseBoolean(System.getProperty("addProp"))){ 
   System.out.println("Sum of two program arguments are: " +
      (Integer.parseInt(args[0])+Integer.parseInt(args[1])));
  }
  else if(Boolean.parseBoolean(System.getProperty("multProp"))){
   System.out.println("Multiplication of two program arguments are: " + 
      (Integer.parseInt(args[0])*Integer.parseInt(args[1])));
  }
 }

}
In eclipse program arguments and VM arguments can be passed from Run configurations.Follow following steps:-
Right click on project -> Go to Run As -> Run configurations.. and click on arguments tab.
Program arguments are added separated by spaces as shown in following diagram (14 12 added separated by spaces).
Similarly, VM arguments can be passed as -DKey = Value pair, we need to append -D in key and value can be any thing. For following program unit we passed two VM arguments as -DaddProp = true and -DmultProp= false
Program and VM arguments passing in eclipse 
 Note:-  "Error Could not find or load main class true"
No space is allowed between "key=value". If we give space between key and value like
-DaddProp = true  or 
DaddProp=  true , while running project or program we witness above error. Correct way of providing VM argument is :  -DKey=Value

Pass program and VM arguments in Net beans:- 

Right click on project and click properties. Click on Run in categories.
In arguments -  add program arguments 14 12 (separated by spaces)  and in VM Options-  add VM arguments (-DaddProp = true -DmultProp= false). Refer following diagram.
Program and VM arguments in Net beans

Pass program and VM arguments in Command line:- 

When we are executing java program form CMD, we can pass VM arguments and program arguments as follows:-
 java <vm_arguments> <java_class_name> <program_arguments>
Above program unit can be executed with VM and Program arguments as follows:-
java -DaddProp=true -DmultProp=false ProgramAndVMParaametersPassing 12 23
Program and VM arguments in command line

Pass VM arguments in pom.xml:- 

Sometimes we need to pass VM arguments via pom.xml. It can be done using "maven-sunfire-plugin". Following plugin node entry can be added to pass VM arguments via pom.xml. Under configuration we are passing two VM arguments -DaddProp=true and -DmultProp=false.
<project>
.....
.......

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <argLine>-DaddProp=true</argLine>
                    <argLine>-DmultProp=false</argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
........
..........
<project>

Location: Hyderabad, Telangana, India