Home »
Java programming language
Java System class getProperties() method with example
System class getProperties() method: Here, we are going to learn about the getProperties() method of System class with its syntax and example.
Submitted by Preeti Jain, on September 16, 2019
System class getProperties() method
Syntax:
public static Properties getProperties();
public static String getProperty(String property_name);
public static String getProperty(String property_name, String default_value);
- getProperties() method is available in java.lang package.
- getProperties() method is used to get the current system properties.
- getProperty(String property_name) method is used to get the system property based on the specified property name (provided in the parameter).
- public static String getProperty(String property_name, String default_value) method is used to get the system property based on the specified property name (provided in the parameter), and it may return the default property if the specified property does not exist.
-
There may following exceptions occur:
- SecurityException: In this exception checkPropertyAccess() method can’t allow access to the given system property when security manager exists.
- NullPointerException: In this exception, if the given system property is null or we can say that the given system property holds null value.
- IllegalArgumentException: In this exception, if the given system property is empty or we can say that the given system property does not hold any value.
Parameter(s):
- In the first case, there is no need to provide any parameter.
- In the second case, property_name – specifies the name of the property to be returned.
- In the third case, property_name – specifies the name of the property to be returned and default_value – specifies the value to be returned, if the given property does not exist.
Return value:
- In the first case, the return type is Properties – it returns the system properties.
- In the second case, the return type is String – it returns the specified property as a string.
- In the third case, the return type is String – it returns the specified property (or default system property) as a string.
Java program to demonstrate the example of getProperties() method
// Java program to demonstrate the example of
// getProperties() method of System Class
import java.lang.*;
import java.util.Properties;
public class GetPropertiesMethod {
public static void main(String[] args) {
System.out.println("Example of getProperties()...");
System.out.println("Display JVM information");
// Property Object
Properties property = System.getProperties();
System.out.println(property);
System.out.println();
System.out.println("Example of getProperties(property_name)...");
// Printing directory
System.out.println("java.vm.name: " + System.getProperty("java.vm.name"));
// Printing library path
System.out.println("java.library.path: " + System.getProperty("java.library.path"));
// Printing name of operating system
System.out.println("os.name: " + System.getProperty("os.name"));
// Printing version of operating system
System.out.println("os.version: " + System.getProperty("os.version"));
System.out.println();
System.out.println("Example of getProperties(property_name, default_value)...");
System.out.println("os.version: " + System.getProperty("os.version", "It's not a property"));
System.out.println("os.java: " + System.getProperty("os.java", "It's not a property"));
}
}
Output
E:\Programs>javac GetPropertiesMethod.java
E:\Programs>java GetPropertiesMethod
Example of getProperties()...
Display JVM information
{awt.toolkit=sun.awt.X11.XToolkit, java.specification.version=10, file.encoding.pkg=sun.io,
sun.cpu.isalist=, sun.jnu.encoding=ANSI_X3.4-1968, java.class.path=*:.,
java.vm.vendor="Oracle Corporation", sun.arch.data.model=64,
java.vendor.url=http://java.oracle.com/, user.timezone=, os.name=Linux,
java.vm.specification.version=10, sun.java.launcher=SUN_STANDARD, user.country=US,
sun.boot.library.path=/usr/lib/jvm/java-10-jdk/lib, sun.java.command=GetPropertiesMethod,
jdk.debug=release, sun.cpu.endian=little, user.home=/root, user.language=en,
java.specification.vendor=Oracle Corporation, java.version.date=2018-04-17,
java.home=/usr/lib/jvm/java-10-jdk, file.separator=/,
java.vm.compressedOopsMode=32-bit, line.separator=,
java.specification.name=Java Platform API Specification,
java.vm.specification.vendor=Oracle Corporation,
java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment,
sun.management.compiler=HotSpot 64-Bit Tiered Compilers, java.runtime.version=10.0.1+10,
user.name=root, path.separator=:, os.version=4.8.0-41-generic,
java.runtime.name=Java(TM) SE Runtime Environment, file.encoding=ANSI_X3.4-1968,
java.vm.name=Java HotSpot(TM) 64-Bit Server VM, java.vendor.version=18.3,
java.vendor.url.bug=http://bugreport.java.com/bugreport/, java.io.tmpdir=/tmp,
java.version=10.0.1, user.dir=/home, os.arch=amd64,
java.vm.specification.name=Java Virtual Machine Specification,
java.awt.printerjob=sun.print.PSPrinterJob, sun.os.patch.level=unknown,
java.library.path=/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib,
java.vendor=Oracle Corporation, java.vm.info=mixed mode, java.vm.version=10.0.1+10,
sun.io.unicode.encoding=UnicodeLittle, java.class.version=54.0}
Example of getProperties(property_name)...
java.vm.name: Java HotSpot(TM) 64-Bit Server VM
java.library.path: /usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib
os.name: Linux
os.version: 4.8.0-41-generic
Example of getProperties(property_name, default_value)...
os.version: 4.8.0-41-generic
os.java: It's not a property