This section provides a tutorial example on how to modify system properties provided by the JVM, and set your own properties into the system property map.
The system properties are actually stored in a map structure, which can also be used
by your application program to store your own properties. For example, the following program
stores two non-system properties at the beginning, and uses them later in the program:
/**
* PropertyTest.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
public class PropertyTest {
public static void main(String[] a) {
setProgramInfo();
printMessage();
}
public static void setProgramInfo() {
// Modifying a system property
System.setProperty("java.io.tmpdir","c:\\var\\tmp");
// Adding my own properties
System.setProperty("program.name","Property Test");
System.setProperty("program.version","3.01");
}
public static void printMessage() {
String userName = System.getProperty("user.name");
String programName = System.getProperty("program.name");
String programVersion = System.getProperty("program.version");
String ioTempDir = System.getProperty("java.io.tmpdir");
System.out.println("Hello "+userName+",");
System.out.println("");
System.out.println("Welcome to \""+programName+", "
+programVersion+"\".");
System.out.println("Note that the Java I/O "
+" temporary directory is located at "+ioTempDir+".");
}
}
Output:
Hello herong,
Welcome to "Property Test, 3.01".
Note that the Java I/O temporary directory is located at c:\var\tmp.