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, HerongYang.com, All Rights Reserved.
 */
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 7 system with JDK 1.7.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:\Program Files\java\jdk1.7.0_45\jre\bin
   java.vm.version = 24.45-b08
   hy.computer.maker = HP
   java.vm.vendor = Oracle Corporation
   java.vendor.url = http://java.oracle.com/
   path.separator = ;
   java.vm.name = Java HotSpot(TM) Client VM
   file.encoding.pkg = sun.io
   user.script = 
   user.country = US
   sun.java.launcher = SUN_STANDARD
   sun.os.patch.level = Service Pack 1
   java.vm.specification.name = Java Virtual Machine Specification
   hy.computer.color = Black
   java.runtime.version = 1.7.0_45-b18
   java.awt.graphicsenv = sun.awt.Win32GraphicsEnvironment
   os.arch = x86
   java.io.tmpdir = C:\Users\herong\AppData\Local\Temp\
   line.separator = 

   java.vm.specification.vendor = Oracle Corporation
   user.variant = 
   os.name = Windows 7
   sun.jnu.encoding = Cp1252
   java.library.path = C:\Program Files\java\jdk1.7.0_45\bin;...
  
   java.specification.name = Java Platform API Specification
   java.class.version = 51.0
   hy.computer.os = Windows
   sun.management.compiler = HotSpot Client Compiler
   os.version = 6.1
   user.home = C:\Users\herong
   user.timezone = 
   java.awt.printerjob = sun.awt.windows.WPrinterJob
   java.specification.version = 1.7
   file.encoding = Cp1252
   user.name = herong
   java.class.path = .
   java.vm.specification.version = 1.7
   sun.arch.data.model = 32
   java.home = C:\Program Files\java\jdk1.7.0_45\jre
   sun.java.command = SystemJvmProperty
   java.specification.vendor = Oracle Corporation
   user.language = en
   awt.toolkit = sun.awt.windows.WToolkit
   java.vm.info = mixed mode, sharing
   java.version = 1.7.0_45
   java.ext.dirs = C:\Program Files\java\jdk1.7.0_45\jre\lib\ext;...
   java.vendor = Oracle Corporation
   file.separator = \
   java.vendor.url.bug = http://bugreport.sun.com/bugreport/
   sun.cpu.endian = little
   sun.io.unicode.encoding = UnicodeLittle
   sun.desktop = windows
   ...

The test result tells me that:

Last update: 2014.

Table of Contents

 About This Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 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 28.2.7 by Oracle Corporation

 JVM Runtime Data Areas

 Memory Management and Garbage Collectors

 Garbage Collection Tests

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 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

 Outdated Tutorials

 References

 PDF Printing Version