management

Obtain System Properties

This is an example of how to obtain the System Properties. We are using the RuntimeMXBean, that is the management interface for the runtime system of the Java virtual machine. Getting the System Properties implies that you should:

  • Get the JVM’s thread system bean, that is the RuntimeMXBean, using the getRuntimeMXBean() API method of ManagementFactory.
  • Get the Map of the properties, using getSystemProperties() API method of RuntimeMXBean.
  • Iterate over the entries of the map and print the properties.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Map;
import java.util.Set;
 
public class SystemProperties {
    public static void main(String[] args) {

  RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean(); 

  // Create a Map that holds all the properties
  Map<String, String> properties = bean.getSystemProperties();
  // Take all the keys from the Map and store them to a Set of keys 
  Set<String> keys = properties.keySet();
  // For every key obtain information
  for (String key : keys) {

String value = properties.get(key);

System.out.println("Property["+ key +"] = " + value);

  }
    }
}

Output:

Property = 21.0-b17
Property[sun.jnu.encoding] = Cp1253
Property = http://java.oracle.com/
Property = mixed mode
Property[user.dir] = C:UsersjavacodegeeksworkspaceEclipseSnippets
Property[sun.cpu.isalist] = amd64
Property = sun.awt.Win32GraphicsEnvironment
Property[sun.os.patch.level] = Service Pack 1
Property = C:UsersjavacodegeeksAppDataLocalTemp
Property[user.home] = C:Usersjavacodegeeks
Property = sun.awt.windows.WPrinterJob
Property = 1.7.0
Property[file.encoding.pkg] = sun.io
Property = http://bugreport.sun.com/bugreport/
Property[file.encoding] = Cp1253
Property[line.separator] = 

Property[sun.java.command] = com.javacodegeeks.snippets.core.SystemProperties
Property = Oracle Corporation
Property = Oracle Corporation
Property = C:UsersjavacodegeeksworkspaceEclipseSnippetsbin
Property[sun.io.unicode.encoding] = UnicodeLittle
Property[user.variant] = 
Property[os.arch] = amd64
Property[user.name] = javacodegeeks
Property[user.language] = en
Property = 1.7.0-b147
Property[sun.desktop] = windows
Property[sun.cpu.endian] = little
Property[awt.toolkit] = sun.awt.windows.WToolkit
Property[sun.boot.library.path] = C:Program FilesJavajre7bin
Property = Java HotSpot(TM) 64-Bit Server VM
Property = C:Program FilesJavajre7
Property = C:Program FilesJavajre7libendorsed
Property[sun.management.compiler] = HotSpot 64-Bit Tiered Compilers
Property = Java(TM) SE Runtime Environment
Property[user.country.format] = GR
Property[file.separator] = 
Property = Oracle Corporation
Property = 1.7
Property[user.language.format] = el
Property[sun.java.launcher] = SUN_STANDARD
Property[user.timezone] = 
Property[os.name] = Windows 7
Property[path.separator] = ;
Property = C:Program FilesJavajre7libext;C:WindowsSunJavalibext
Property[sun.arch.data.model] = 64
Property = Java Platform API Specification
Property[os.version] = 6.1
Property[user.script] = 
Property[user.country] = US
Property = 51.0
Property = Oracle Corporation
Property = Java Virtual Machine Specification
Property = 1.7

 
This was an example of how to obtain the System Properties in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button