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:

 * Copyright (c) All Rights Reserved.
class SystemJvmProperty {
   public static void main(String[] a) { out = System.out;

      // adding my own entries to JVM properties

      // 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 SystemJvmProperty = 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 =
   path.separator = ; = Java HotSpot(TM) 64-Bit Server VM
   file.encoding.pkg =
   sun.os.patch.level = Service Pack 1
   user.script = = US = SUN_STANDARD = Java Virtual Machine Specification = Black
   java.vm.compressedOopsMode = 32-bit
   java.runtime.version = 10.0.1+10
   java.awt.graphicsenv = sun.awt.Win32GraphicsEnvironment
   os.arch = amd64 = C:\Users\herong\AppData\Local\Temp\
   line.separator =

   java.vm.specification.vendor = Oracle Corporation
   user.variant = = 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 Platform API Specification = Windows = HotSpot 64-Bit Tiered Compilers
   os.version = 6.1
   user.home = C:\Users\herong
   user.timezone =
   java.awt.printerjob =
   file.encoding = Cp1252
   java.specification.version = 10 = herong
   java.class.path = .
   java.vm.specification.version = 10 = 64 = SystemJvmProperty
   java.home = C:\Progra~1\Java\jdk-10.0.1
   user.language = en
   java.specification.vendor = Oracle Corporation
   awt.toolkit = = mixed mode
   java.version = 10.0.1
   java.vendor = Oracle Corporation
   sun.stderr.encoding = cp437
   file.separator = \ = 2018-04-17
   java.vendor.url.bug = = 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


 Full Version in PDF/EPUB