Java System.getProperty()

Filed Under: Java

Java System.getProperty method is very useful to find out the operating system information.

Java System.getProperty

java System getProperty, System.getProperty in java

We can use System.getProperty() method to find out the Operating System information, user home directory, Java runtime version, path separator, line separator, user working directory, Java home, Java classpath, and various other useful information.

Java System getProperty Example

Java System class getProperties() method returns the Set of keys for the properties and here we will use it to list out all the system properties.


package com.journaldev.util;

import java.util.Properties;
import java.util.Set;

public class JavaSystemGetProperty {

	public static void main(String[] args) {
		Properties prop = System.getProperties();
		Set<Object> keySet = prop.keySet();
		for (Object obj : keySet) {
			System.out.println("System Property: {" 
					+ obj.toString() + "," 
					+ System.getProperty(obj.toString()) + "}");
		}
	}

}

The output of the above System getProperty example program is;


System Property: {java.runtime.name,Java(TM) SE Runtime Environment}
System Property: {sun.boot.library.path,/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib}
System Property: {java.vm.version,25.131-b11}
System Property: {user.country.format,IN}
System Property: {gopherProxySet,false}
System Property: {java.vm.vendor,Oracle Corporation}
System Property: {java.vendor.url,http://java.oracle.com/}
System Property: {path.separator,:}
System Property: {java.vm.name,Java HotSpot(TM) 64-Bit Server VM}
System Property: {file.encoding.pkg,sun.io}
System Property: {user.country,US}
System Property: {sun.java.launcher,SUN_STANDARD}
System Property: {sun.os.patch.level,unknown}
System Property: {java.vm.specification.name,Java Virtual Machine Specification}
System Property: {user.dir,/Users/pankaj/Documents/eclipse-workspace/JavaExceptions}
System Property: {java.runtime.version,1.8.0_131-b11}
System Property: {java.awt.graphicsenv,sun.awt.CGraphicsEnvironment}
System Property: {java.endorsed.dirs,/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/endorsed}
System Property: {os.arch,x86_64}
System Property: {java.io.tmpdir,/var/folders/1t/sx2jbcl534z88byy78_36ykr0000gn/T/}
System Property: {line.separator,
}
System Property: {java.vm.specification.vendor,Oracle Corporation}
System Property: {os.name,Mac OS X}
System Property: {sun.jnu.encoding,UTF-8}
System Property: {java.library.path,/Users/pankaj/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.}
System Property: {java.specification.name,Java Platform API Specification}
System Property: {java.class.version,52.0}
System Property: {sun.management.compiler,HotSpot 64-Bit Tiered Compilers}
System Property: {os.version,10.12.6}
System Property: {http.nonProxyHosts,local|*.local|169.254/16|*.169.254/16}
System Property: {user.home,/Users/pankaj}
System Property: {user.timezone,}
System Property: {java.awt.printerjob,sun.lwawt.macosx.CPrinterJob}
System Property: {file.encoding,UTF-8}
System Property: {java.specification.version,1.8}
System Property: {java.class.path,/Users/pankaj/Documents/eclipse-workspace/JavaExceptions/bin:/Users/pankaj/temp/commons-io-2.5.jar}
System Property: {user.name,pankaj}
System Property: {java.vm.specification.version,1.8}
System Property: {sun.java.command,com.journaldev.util.JavaSystemGetProperty}
System Property: {java.home,/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre}
System Property: {sun.arch.data.model,64}
System Property: {user.language,en}
System Property: {java.specification.vendor,Oracle Corporation}
System Property: {awt.toolkit,sun.lwawt.macosx.LWCToolkit}
System Property: {java.vm.info,mixed mode}
System Property: {java.version,1.8.0_131}
System Property: {java.ext.dirs,/Users/pankaj/Library/Java/Extensions:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java}
System Property: {sun.boot.class.path,/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/sunrsasign.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/classes}
System Property: {java.vendor,Oracle Corporation}
System Property: {file.separator,/}
System Property: {java.vendor.url.bug,http://bugreport.sun.com/bugreport/}
System Property: {sun.io.unicode.encoding,UnicodeBig}
System Property: {sun.cpu.endian,little}
System Property: {socksNonProxyHosts,local|*.local|169.254/16|*.169.254/16}
System Property: {ftp.nonProxyHosts,local|*.local|169.254/16|*.169.254/16}
System Property: {sun.cpu.isalist,}

Some of the valuable information being printed above are;

  1. The program is running on Mac OS, see output {os.name,Mac OS X}
  2. Java version is 1.8.0_131, see system property output {java.version,1.8.0_131}
  3. User name is pankaj, as seen in output {user.name,pankaj}
  4. Path separator is colon (:), see output {path.separator,:}
  5. It’s a 64-bit OS, see output {os.arch,x86_64}

There are many more useful information printed too, you should check them out.

We should use this System information where we need to make sure our program is platform independent, for example creating the file path we should use file.separator property and to read PATH information we should use path.separator variable. That’s all about System.getProperty in java.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages