How do I set System properties?
Author: Deron Eriksson
Description: This Java tutorial shows how to set System properties.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1

Page:    1 2 >

Programmatically, a system property can be set using the setProperty method of the System object, and also via the setProperty method of the Properties object that can be obtained from System via getProperties. In addition, system properties can be set via JavaSW virtual machine arguments when you start up your application. This tutorial will demonstrate these different ways.

The SetSystemProperties class programmatically creates the "favorite.berry" and "favorite.donut" system properties and sets values for these properties. It then displays values for the "favorite.berry", "favorite.donut", "", and "" properties.

package test;

import java.util.Properties;

public class SetSystemProperties {
	public static void main(String[] args) {
		System.setProperty("favorite.berry", "blueberry");

		Properties systemProperties = System.getProperties();
		systemProperties.setProperty("favorite.donut", "apple fritter");

		String favoriteBerry = System.getProperty("favorite.berry");
		String favoriteDonut = systemProperties.getProperty("favorite.donut");
		String favoriteDay = System.getProperty("");
		String favoriteCar = System.getProperty("");
		System.out.println("My favorite berry is: " + favoriteBerry);
		System.out.println("My favorite donut is: " + favoriteDonut);
		System.out.println("My favorite day is: " + favoriteDay);
		System.out.println("My favorite car is: " + favoriteCar);

The execution of SetSystemProperties is shown below.

SetSystemProperties execution

As you can see, the "favorite.berry" and "favorite.donut" properties return results but "" and "" return null. This is because these last two properties haven't been set. Let's set these properties via arguments to the JVMW. Here is a debug configuration for the SetSystemProperties class.

SetSystemProperties Debug Configuration

(Continued on page 2)

Page:    1 2 >