System.getProperty() in Java

System.getProperty(): This method is available in java.lang.System class of Java.

Approach 1: When the method takes one argument.

Syntax:

String java.lang.System.getProperty(String key)

This method takes one argument of type String as its parameter. This method gets the system property indicated by the specified key. First, if there is a security manager, its checkPropertyAccess. This may result in a SecurityException.

Parameters: One parameter is required for this method.

key: the name of the system property.

Returns: the string value of the system property, or null if there is no property with that key.

Throws:

1. SecurityException - if a security manager exists and its checkPropertyAccess method doesn't allow access to the specified system property.

2. NullPointerException - if the key is null.

3. IllegalArgumentException - if the key is empty.

Java

public class SystemgetProperty {
    public static void main(String[] args) {

        String key = "os.name";
        System.out.println(System.getProperty(key));

    }
}

Output:

Windows 10


Approach 1.1: NullPointerException

Java

public class SystemgetProperty {
    public static void main(String[] args) {

        String key = null;
        System.out.println(System.getProperty(key));

    }
}


Output:

Exception in thread "main" java.lang.NullPointerException: key can't be null at java.base/java.lang.System.checkKey(System.java:960) at java.base/java.lang.System.getProperty(System.java:830)



Approach 1.2: IllegalArgumentException 

Java

public class SystemgetProperty {
    public static void main(String[] args) {

        String key = "";
        System.out.println(System.getProperty(key));

    }
}


Output:

Exception in thread "main" java.lang.IllegalArgumentException: key can't be empty at java.base/java.lang.System.checkKey(System.java:963) at java.base/java.lang.System.getProperty(System.java:830)


Approach 2: When the method takes two arguments.

Syntax:

String java.lang.System.getProperty(String key, String def)

This method takes two arguments of type String as its parameters. This method gets the system property indicated by the specified key. First, if there is a security manager, its checks property Access.

Parameters: Two parameters are required for this method.

key: the name of the system property.

def: a default value.

Returns: the string value of the system property, or the default value if there is no property with that key.

Throws:

1. SecurityException - if a security manager exists and its checkPropertyAccess method doesn't allow access to the specified system property.

2. NullPointerException - if the key is null.

3. IllegalArgumentException - if the key is empty.

Java

public class SystemgetProperty2 {
    public static void main(String[] args) {

        String key = "os.name";
        String def = "new system property";
        System.out.println(System.getProperty(key, def));
    }
}

Output:

Windows 10


Approach 2.1: NullPointerException

Java

public class SystemgetProperty2 {
    public static void main(String[] args) {

        String key = null;
        String def = "new system property";
        System.out.println(System.getProperty(key, def));
    }
}


Output:

Exception in thread "main" java.lang.NullPointerException: key can't be null at java.base/java.lang.System.checkKey(System.java:960) at java.base/java.lang.System.getProperty(System.java:865)



Approach 2.2: IllegalArgumentException

Java

public class SystemgetProperty2 {
    public static void main(String[] args) {

        String key = "";
        String def = "new system property";
        System.out.println(System.getProperty(key, def));
    }
}


Output:

Exception in thread "main" java.lang.IllegalArgumentException: key can't be empty at java.base/java.lang.System.checkKey(System.java:963) at java.base/java.lang.System.getProperty(System.java:865)


No comments:

Post a Comment