System.load() in Java

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

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 method is called with the key asits argument. 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.

Approach 1: When no exceptions.

Java

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

        String key = "D:\\Java\\sts-4.9.0.RELEASE\\readme";
        System.load(key);

        System.out.println("File loaded succesfully");
    }
}

Output:

File loaded successfully


Approach 2: NullPointerException 

Java

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

        String key = null;
        System.load(key);

        System.out.println("File loaded succesfully");
    }
}


Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.io.File.<init>(File.java:279) at java.base/java.lang.Runtime.load0(Runtime.java:742) at java.base/java.lang.System.load(System.java:1857)


No comments:

Post a Comment