In this guide, we will discuss System.getProperty() method in Java. The getProperty()
method of System
class is frequently used to retrieve various system properties such as java version, os version, java home directory details etc.
Program to get system properties using System.getProperty()
Let’s write a java program to get system properties using System.getProperty()
method and print them.
public class SystemPropertiesExample {
public static void main(String[] args) {
// Getting various system properties
String javaVersion = System.getProperty("java.version");
String javaVendor = System.getProperty("java.vendor");
String javaVendorURL = System.getProperty("java.vendor.url");
String javaHome = System.getProperty("java.home");
String javaClassPath = System.getProperty("java.class.path");
String osName = System.getProperty("os.name");
String osVersion = System.getProperty("os.version");
String osArch = System.getProperty("os.arch");
String userName = System.getProperty("user.name");
String userHome = System.getProperty("user.home");
String userDir = System.getProperty("user.dir");
// Printing the system properties
System.out.println("Java Version: " + javaVersion);
System.out.println("Java Vendor: " + javaVendor);
System.out.println("Java Vendor URL: " + javaVendorURL);
System.out.println("Java Home: " + javaHome);
System.out.println("Java Class Path: " + javaClassPath);
System.out.println("OS Name: " + osName);
System.out.println("OS Version: " + osVersion);
System.out.println("OS Architecture: " + osArch);
System.out.println("User Name: " + userName);
System.out.println("User Home: " + userHome);
System.out.println("User Working Directory: " + userDir);
}
}
Output:
Java Version: 1.8.0_291
Java Vendor: Oracle Corporation
Java Vendor URL: http://java.oracle.com/
Java Home: C:\Program Files\Java\jdk1.8.0_291\jre
Java Class Path: .;C:\path\to\your\classes;C:\path\to\your\libraries\lib.jar
OS Name: Windows 10
OS Version: 10.0
OS Architecture: amd64
User Name: chaitanya.singh
User Home: C:\Users\chaitanya.singh
User Working Directory: C:\Users\chaitanya.singh\workspace\MyProject
Notes
- The output will vary depending on the operating system and java version you are running.
- If any of the mentioned property is not available, the corresponding output line might be empty or null.
Explanation of Each Property
java.version
: The Java version.java.vendor
: The vendor information from where you got the java installed.java.vendor.url
: The url to the vendor’s website.java.home
: complete path to the directory where JRE is installed.java.class.path
: The class path used by the system.os.name
: OS name.os.version
: The version of the running OS.os.arch
: The architecture of the operating system (e.g., x86, amd64).user.name
: The user’s account name.user.home
: The user’s home directory path.user.dir
: The user’s current working folder/directory.
Leave a Reply