How to Get System Properties in Java?

Filed Under: Java
how to find system properties in Java

The System class in Java maintains a set of properties. These properties are stored in the form of key/value pairs. Both keys and values are Strings that define traits or attributes of the current working environment.

There are two methods that you can use to read the system properties: getProperty() and getProperties().

Getting All System Properties in Java

System.getProperties() returns an Enumeration of all the system properties. The following code prints all the system properties on the console.

import java.util.Enumeration;
import java.util.Properties;

public class Main {

    public static void main(String[] args)
    {
        Properties p = System.getProperties();
        Enumeration keys = p.keys();
        while (keys.hasMoreElements()) {
            String key = (String)keys.nextElement();
            String value = (String)p.get(key);
            System.out.println(key + ": " + value);
        }
    }
}
System properties
Output

The Output in text form:

gopherProxySet: false
awt.toolkit: sun.lwawt.macosx.LWCToolkit
java.specification.version: 11
sun.cpu.isalist: 
sun.jnu.encoding: UTF-8
java.class.path: /Users/jayant/Desktop/java/JD1/out/production/JD1
java.vm.vendor: Oracle Corporation
sun.arch.data.model: 64
java.vendor.url: http://java.oracle.com/
user.timezone: 
os.name: Mac OS X
java.vm.specification.version: 11
sun.java.launcher: SUN_STANDARD
user.country: GB
sun.boot.library.path: /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/lib
sun.java.command: Main
http.nonProxyHosts: local|*.local|169.254/16|*.169.254/16
jdk.debug: release
sun.cpu.endian: little
user.home: /Users/jayant
user.language: en
java.specification.vendor: Oracle Corporation
java.version.date: 2018-10-16
java.home: /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
file.separator: /
java.vm.compressedOopsMode: Zero based
line.separator: 

java.specification.name: Java Platform API Specification
java.vm.specification.vendor: Oracle Corporation
java.awt.graphicsenv: sun.awt.CGraphicsEnvironment
sun.management.compiler: HotSpot 64-Bit Tiered Compilers
ftp.nonProxyHosts: local|*.local|169.254/16|*.169.254/16
java.runtime.version: 11.0.2+7-LTS
user.name: jayant
path.separator: :
os.version: 10.14.2
java.runtime.name: Java(TM) SE Runtime Environment
file.encoding: UTF-8
java.vm.name: Java HotSpot(TM) 64-Bit Server VM
java.vendor.version: 18.9
java.vendor.url.bug: http://bugreport.java.com/bugreport/
java.io.tmpdir: /var/folders/56/fc29wjz520x_21fmrl9r2jgc0000gn/T/
java.version: 11.0.2
user.dir: /Users/jayant/Desktop/java/JD1
os.arch: x86_64
java.vm.specification.name: Java Virtual Machine Specification
java.awt.printerjob: sun.lwawt.macosx.CPrinterJob
sun.os.patch.level: unknown
java.library.path: /Users/jayant/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
java.vendor: Oracle Corporation
java.vm.info: mixed mode
java.vm.version: 11.0.2+7-LTS
sun.io.unicode.encoding: UnicodeBig
java.class.version: 55.0
socksNonProxyHosts: local|*.local|169.254/16|*.169.254/16

Process finished with exit code 0

Important System Properties

Some of the important system properties are:

“file.separator”File separator (for example, “/”)
“java.class.path”Java classpath
“java.class.version”Java class version number
“java.home”Java installation directory
“java.vendor”Java vendor-specific string
“java.vendor.url”Java vendor URL
“java.version”Java version number
“line.separator”Line separator
“os.arch”Operating system architecture
“os.name”Operating system name
“os.version”Operating system version
“path.separator”Path separator (for example, “:”)
“user.language”Language used by User
“user.dir”User’s current working directory
“user.home”User home directory
“user.name”User account name

Getting a Specific System Property

To get a particular property from the list use System.Property(key). Where key is the name of the property you want to retrieve. The output is returned as a string. If the property key doesn’t match then null is returned.

public class Main {

    public static void main(String[] args)
    {
        System.out.println(System.getProperty("java.class.path"));
        System.out.println(System.getProperty("os.name"));
        System.out.println(System.getProperty("user.name"));
    }
}
Output Properties
Output
/Users/jayant/Desktop/java/JD1/out/production/JD1
Mac OS X
jayant

The three properties have been printed out.

There is another variation that lets you specify what has to be printed in case the property name doesn’t match. Observe the difference in the fourth and fifth line in the following :

public class Main {

    public static void main(String[] args)
    {
System.out.println(System.getProperty("java.class.path"));
System.out.println(System.getProperty("os.name"));
System.out.println(System.getProperty("user.name"));
System.out.println(System.getProperty("hello"));
System.out.println(System.getProperty("hello","property not found"));
    }
}
printing properties
Output
/Users/jayant/Desktop/java/JD1/out/production/JD1
Mac OS X
jayant
null
property not found

The fourth line returns null, since “hello” doesn’t match with any property name. The fifth line returns the line we mentioned in the code “property not found”.

Conclusion

We can retrieve system properties using the above-mentioned methods. Information such as the version of Java in use, home directory, name of Java vendor can be retrieved from the System properties.

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