getproperty() and getproperties() methods in Java

In this tutorial, you will learn about getproperty() and getproperties() in Java. Before knowing about getproperty() and getproperties() let us see about a system(). The Java system() class provides access to system resources. System class has many uses that include standard input, standard output, and error output stream. These play a vital role in reading inputs and showing outputs. System class also helps in knowing about the system security interface.

To fetch system information or properties we have two methods. They are:

  • getProperty()
  • getProperties()

Description

These two methods key-value pair property. In other words, you should give the property name as a key to get it from a system that will be fetched as value.

getProperty() in Java

getProperty() method gets the single property at a time from the system. Therefore you should pass a key whose system property we want. After that, it will return the value of the property if it is present. Otherwise, it returns Null.

Note: Import java lang package

SYNTAX:System.getProperty(“key”)
import java.util.Properties;

class v
{
public static void main(String args[])
{
System.out.println(System.getProperty("os.name"));
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("user.name"));
}
}
Windows 10
13.0.1
xxxxxx

You can set a default value for a specific key using getProperty(). But here you have to pass two arguments to the method. In other words, you should only pass both keys and values.

SYNTAX:System.getProperty(“key”,”value”)
import java.util.Properties;

class v
{
public static void main(String args[])
{
System.out.println(System.getProperty("speedy","code"));
System.out.println(System.getProperty("Clarity","codespeedy"));
}
}
code
codespeedy

getProperties() in Java

getProperties() method collects all information from the system using Java Virtual Machine including its version. It will return an object. This contains a set of system properties. Therefore to obtain all properties at once we use getProperties().

SYNTAX:System.getProperties()
import java.util.Properties;

class v1
{
public static void main(String args[])
{
System.out.println(System.getProperties());
}
}
{java.specification.version=13, sun.cpu.isalist=amd64, sun.jnu.encoding=Cp1252, java.class.path=., java.vm.vendor=Oracle Corporation, sun.arch.data.model=64, user.variant=, java.vendor.url=https://java.oracle.com/, java.vm.specification.version=13, os.name=Windows 10, sun.java.launcher=SUN_STANDARD, user.country=IN,sun.java.command=v1, jdk.debug=release, sun.cpu.endian=little, user.home=C:\Users\srivi, user.language=en, sun.stderr.encoding=cp437, java.specification.vendor=Oracle Corporation, java.version.date=2019-10-15, java.home=C:\Program Files\Java\jdk-13.0.1, file.separator=\, java.vm.compressedOopsMode=Zero based, line.separator=
, sun.stdout.encoding=cp437, java.vm.specification.vendor=Oracle Corporation, java.specification.name=Java Platform API Specification, user.script=, sun.management.compiler=HotSpot 64-Bit Tiered Compilers, java.runtime.version=13.0.1+9,path.separator=;, os.version=10.0, java.runtime.name=Java(TM) SE Runtime Environment, file.encoding=Cp1252, java.vm.name=Java HotSpot(TM) 64-Bit Server VM, java.vendor.url.bug=https://bugreport.java.com/bugreport/, java.vm.info=mixed mode, sharing, java.vendor=Oracle Corporation, java.vm.version=13.0.1+9, sun.io.unicode.encoding=UnicodeLittle, java.class.version=57.0}

Conclusion

getProperty() and getProperties() are methods from System class in Java. These methods are used to fetch system properties in the form of key-value pairs. You can implement them in the above-mentioned way. These methods have very much importance while writing system based programs.

To know more about the methods of Systems class

Also, read Difference between Inheritance and Composition in Java

Leave a Reply