Java System Properties Sample Code

The Java System class comes with some very useful methods when it comes to System Properties. The Java platform uses a Properties object to maintain its own configuration. The Properties object describes the configuration of the current working environment. System properties include information about the current user, the current version of the Java Runtime environment, and many other platform property configurations.

Class Name Method Parameters Return Type Exceptions
System getProperties() none java.util.Property SecurityException
System getProperty() String String SecurityException
NullPointerException
IllegalArgumentException

Java System Properties Source Code:

package com.jcd.java.tutorials;

import java.util.Properties;

public class JavaSystemProperties 
{

	public static void main(String[] args) 
	{

		//Get and print out in bulk format the current system properties
		System.out.println("\nSystem Properties Set: "+System.getProperties().size());
		
		//Get and print our a specific system property
		System.out.println("\nJava Version: "+System.getProperty("java.version"));
		
		//Set a dummy System Property
		System.setProperty("DUMMY", "My Dummy System Property Value");
		
		//Get the dummy System Property
		System.out.println("\nDummy Property: "+System.getProperty("DUMMY"));
		
		//Print out all System Properties In More Readable Format
		Properties sysProperties = System.getProperties();
		System.out.println("\n----------------------------------------------------------");
		sysProperties.list(System.out);

	}

}



Sample Java System Properties Output:

System Properties Set: 54

Java Version: 1.8.0_151

Dummy Property: My Dummy System Property Value

----------------------------------------------------------
-- listing properties --
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=C:\Program Files\Java\jre1.8.0_151\bin
java.vm.version=25.151-b12
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
user.script=
user.country=US
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=
java.vm.specification.name=Java Virtual Machine Specification
user.dir=C:\Users\Admin\Documents\Workspace\Tu...
java.runtime.version=1.8.0_151-b12
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=C:\Program Files\Java\jre1.8.0_151\li...
os.arch=amd64
java.io.tmpdir=C:\Users\Admin\AppData\Local\Temp\
line.separator=

DUMMY=My Dummy System Property Value
java.vm.specification.vendor=Oracle Corporation
user.variant=
os.name=Windows 10
sun.jnu.encoding=Cp1252
java.library.path=C:\Program Files\Java\jre1.8.0_151\bi...
java.specification.name=Java Platform API Specification
java.class.version=52.0
sun.management.compiler=HotSpot 64-Bit Tiered Compilers
os.version=10.0
user.home=C:\Users\Admin
user.timezone=
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=Cp1252
java.specification.version=1.8
user.name=Admin
java.class.path=C:\Program Files\Java\jre1.8.0_151\li...
java.vm.specification.version=1.8
sun.arch.data.model=64
java.home=C:\Program Files\Java\jre1.8.0_151
sun.java.command=com.jcd.java.tutorials.JavaSystemProp...
java.specification.vendor=Oracle Corporation
user.language=en
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode
java.version=1.8.0_151
java.ext.dirs=C:\Program Files\Java\jre1.8.0_151\li...
sun.boot.class.path=C:\Program Files\Java\jre1.8.0_151\li...
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
sun.cpu.isalist=amd64



References:

Add a Comment

Your email address will not be published. Required fields are marked *