Sunday, July 15, 2012


What's the difference between program arguments and VM arguments?
Author: Deron Eriksson
Description: This tutorial gives an example of the difference between program arguments and VM arguments.
Tutorial created using: Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1)


EclipseSW allows you to enter Program arguments and VM arguments into a Debug configuration. I have a Debug configuration for my ArgsTest class, and I pass in "pro1 pro2 pro3" as Program arguments and "-DsysProp1=sp1 -DsysProp2=sp2" as VM arguments.
ArgsTest Debug Configuration
Program arguments are arguments that are passed to your application, which are accessible via the "args" String array parameter of your main method. VM arguments are arguments such as System properties that are passed to the JavaSW interpreter. The Debug configuration above is essentially equivalent to:
java -DsysProp1=sp1 -DsysProp2=sp2 test.ArgsTest pro1 pro2 pro3
The VM arguments go after the call to your Java interpreter (ie, 'java') and before the Java class. Program arguments go after your Java class.
The ArgsTest.java class is shown below. It lists the program arguments, and it displays the names and values of two VM arguments.

ArgsTest.java

package test;

import java.io.IOException;

public class ArgsTest {

	public static void main(String[] args) throws IOException {

		System.out.println("Program Arguments:");
		for (String arg : args) {
			System.out.println("\t" + arg);
		}

		System.out.println("System Properties from VM Arguments");
		String sysProp1 = "sysProp1";
		System.out.println("\tName:" + sysProp1 + ", Value:" + System.getProperty(sysProp1));
		String sysProp2 = "sysProp2";
		System.out.println("\tName:" + sysProp2 + ", Value:" + System.getProperty(sysProp2));

	}
}
If we execute the Debug configuration for ArgsTest, we see the following output:

Console output

Program Arguments:
	pro1
	pro2
	pro3
System Properties from VM Arguments
	Name:sysProp1, Value:sp1
	Name:sysProp2, Value:sp2
Entering "java -DsysProp1=sp1 -DsysProp2=sp2 test.ArgsTest pro1 pro2 pro3" on the command line from the project bin directory yields the same results.
'java -DsysProp1=sp1 -DsysProp2=sp2 test.ArgsTest pro1 pro2 pro3' on command line

No comments: