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) 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 computer with JDK 10, I got this result:

herong> java -Dhy.computer.color=Black SystemJvmProperty

   java.runtime.name = Java(TM) SE Runtime Environment
   java.vm.version = 10.0.1+10
   sun.boot.library.path = C:\Progra~1\Java\jdk-10.0.1\bin
   java.vm.vendor = "Oracle Corporation"
   java.vendor.url = http://java.oracle.com/
   path.separator = ;
   java.vm.name = Java HotSpot(TM) 64-Bit Server VM
   file.encoding.pkg = sun.io
   sun.os.patch.level = Service Pack 1
   user.script =
   user.country = US
   sun.java.launcher = SUN_STANDARD
   java.vm.specification.name = Java Virtual Machine Specification
   hy.computer.color = Black
   java.vm.compressedOopsMode = 32-bit
   java.runtime.version = 10.0.1+10
   java.awt.graphicsenv = sun.awt.Win32GraphicsEnvironment
   os.arch = amd64
   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:\Progra~1\Java\jdk-10.0.1\bin;...
   jdk.debug = release
   java.class.version = 54.0
   java.specification.name = Java Platform API Specification
   hy.computer.os = Windows
   sun.management.compiler = HotSpot 64-Bit Tiered Compilers
   os.version = 6.1
   user.home = C:\Users\herong
   user.timezone =
   java.awt.printerjob = sun.awt.windows.WPrinterJob
   file.encoding = Cp1252
   java.specification.version = 10
   user.name = herong
   java.class.path = .
   java.vm.specification.version = 10
   sun.arch.data.model = 64
   sun.java.command = SystemJvmProperty
   java.home = C:\Progra~1\Java\jdk-10.0.1
   user.language = en
   java.specification.vendor = Oracle Corporation
   awt.toolkit = sun.awt.windows.WToolkit
   java.vm.info = mixed mode
   java.version = 10.0.1
   java.vendor = Oracle Corporation
   sun.stderr.encoding = cp437
   file.separator = \
   java.version.date = 2018-04-17
   java.vendor.url.bug = http://bugreport.java.com/bugreport/
   sun.io.unicode.encoding = UnicodeLittle
   sun.cpu.endian = little
   java.vendor.version = 18.3
   sun.stdout.encoding = cp437
   sun.desktop = windows
   sun.cpu.isalist = amd64
   ...

The test result tells me that:

Table of Contents

 About This Book

 JVM (Java Virtual Machine) Specification

 Java HotSpot VM - JVM by Oracle/Sun

 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

 JVM Runtime Data Areas

 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

 OpenJ9 by Eclipse Foundation

 JRockit JVM 28.2.7 by Oracle Corporation

 Archived Tutorials

 References

 Full Version in PDF/EPUB