System.setProperty() - Setting Your Own Properties

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) HerongYang.com. All Rights Reserved.
 */
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.

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Control Flow Statements

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 Enum Types and Enum Constants

 StringBuffer - The String Buffer Class

System Properties and Runtime Object Methods

 JVM and OS System Properties

System.setProperty() - Setting Your Own Properties

 Runtime.getRuntime() - Getting the Runtime Object

 freeMemory() - Getting JVM Free Memory Information

 Calculating Memory Usage of an Array

 exec() - Executing Operating System Commands

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Java Modules - Java Package Aggregation

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB