JVM Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.10

Getting and Adding System Properties

This section provides a tutorial example on how to access system properties provided by the JVM instance and how to add your own properties.

The java.lang.System class also allows you to manage a collection of properties stored at the JVM instance level. Whey your application is started, JVM will add many built-in properties into the collection. While your application is running, you can add, retrieve, and remove any properties.

You can also reset or add any properties when invoking the JVM using the -D command line option.

Here is tutorial example program showing you how to manage system properties:

/**
 * SystemJvmProperty.java
 * Copyright (c) 2010 by Dr. Herong Yang, herongyang.com
 */
class SystemJvmProperty {
   public static void main(String[] a) {
      java.io.PrintStream out = System.out;

      // adding my own entries to JVM properties
      System.setProperty("hy.computer.maker","HP");
      System.setProperty("hy.computer.os","Windows");

      // copying all JVM properties into a Properties object
      java.util.Properties props = System.getProperties();
      java.util.Enumeration e = props.propertyNames();
      
      out.println("JVM Properties:");
      while (e.hasMoreElements()) {
         String k = (String) e.nextElement();
         String v = props.getProperty(k);
         out.println("   "+k+" = "+v);
      }
   }
}

When executed on my Windows XP system with JDK 1.6.0, I got this result:

C:\herong\jvm>java -Dhy.computer.color=Black SystemJvmProperty

JVM Properties:
   java.runtime.name = Java(TM) SE Runtime Environment
   sun.boot.library.path = C:\local\jdk\jre\bin
   java.vm.version = 10.0-b22
   hy.computer.maker = HP
   java.vm.vendor = Sun Microsystems Inc.
   java.vendor.url = http://java.sun.com/
   path.separator = ;
   java.vm.name = Java HotSpot(TM) Client VM
   file.encoding.pkg = sun.io
   sun.java.launcher = SUN_STANDARD
   sun.os.patch.level = Service Pack 3
   java.vm.specification.name = Java Virtual Machine Specification
   user.dir = C:\herong\jvm
   hy.computer.color = Black
   java.runtime.version = 1.6.0_06-b02
   java.awt.graphicsenv = sun.awt.Win32GraphicsEnvironment
   os.arch = x86
   java.io.tmpdir = C:\Temp\
   line.separator = 

   java.vm.specification.vendor = Sun Microsystems Inc.
   user.variant = 
   os.name = Windows XP
   sun.jnu.encoding = Cp1252
   java.library.path = C:\local\jdk\bin;.;C:\WINDOWS\system32;C:\WINDOWS
   java.specification.name = Java Platform API Specification
   java.class.version = 50.0
   hy.computer.os = Windows
   sun.management.compiler = HotSpot Client Compiler
   os.version = 5.1
   user.home = C:\Documents and Settings\herong
   user.timezone = 
   java.awt.printerjob = sun.awt.windows.WPrinterJob
   java.specification.version = 1.6
   file.encoding = Cp1252
   user.name = herong
   java.class.path = .
   java.vm.specification.version = 1.0
   sun.arch.data.model = 32
   java.home = C:\local\jdk\jre
   java.specification.vendor = Sun Microsystems Inc.
   user.language = en
   awt.toolkit = sun.awt.windows.WToolkit
   java.vm.info = mixed mode, sharing
   java.version = 1.6.0_06
   java.ext.dirs = C:\local\jdk\jre\lib\ext
   sun.boot.class.path = C:\local\jdk\jre\lib\resources.jar;...
   java.vendor = Sun Microsystems Inc.
   file.separator = \
   java.vendor.url.bug = http://java.sun.com/cgi-bin/bugreport.cgi
   sun.cpu.endian = little
   sun.io.unicode.encoding = UnicodeLittle
   sun.desktop = windows
   ...

The test result tells me that:

  • The JVM indeed provided many built-in properties for me.
  • The "-Dhy.computer.color=Black" option did insert a new property for me.
  • The System.addProperty() method worked correctly.
  • Some built-in properties controls default behaviors of some system classes. For example, "file.encoding = Cp1252" sets the default encoding when reading from files or writing to files as characters.

Last update: 2010.

Table of Contents

 About This Book

 Download and Install Java SE 1.6 Update 2

 java.lang.Runtime Class - The JVM Instance

java.lang.System Class - The Operating System

 What Is java.lang.System?

 Standard Input, Output, and Error Streams

 Current Time in Milliseconds and Nanoseconds

 Accessing System Environment Variables

Getting and Adding System Properties

 ClassLoader Class - Class Loaders

 Class Class - Class Reflections

 Sun's JVM - Java HotSpot VM

 JRockit JVM 7.0 by BEA Systems

 JRockit JVM 8.0 by BEA Systems

 Memory Management Rules and Tests

 Garbage Collection Tests

 Stack Overflow Tests

 Thread Testing Program and Result

 StringBuffer Testing Program and Result

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2010
Getting and Adding System Properties