Open In App

Provider getProperty() method in Java with Examples

Last Updated : 04 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The getProperty() method of java.security.Provider class is used to search for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns null if the property is not found.

Syntax:

public String getProperty(String key)

Parameters: This method takes the property key as parameter.

Return Value: This method returns the value in this property list with the specified key value or null if the property is not found.

Below are the examples to illustrate the getProperty() method:

Example 1:




// Java program to demonstrate
// getProperty() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        // Declaring int values
        int i = 10;
  
        try {
            // creating the object of  KeyPairGenerator
            KeyPairGenerator sr = KeyPairGenerator.getInstance("DSA", "SUN");
  
            // getting the Provider of the KeyPairGenerator sr
            // by using method getProvider()
            Provider provider = sr.getProvider();
  
            // Declaring the variable of set<Map> type
            Set<Object> set;
  
            // getting unmodifiable Set view of the property entries
            set = provider.keySet();
  
            // Creating the object of iterator to iterate set
            Iterator iter = set.iterator();
  
            while (i > 0) {
  
                // getting the mapped value in element
                // using getProperty() method
                String property = provider.getProperty((String)iter.next());
  
                // printing the property of specified key
                System.out.println("value is : " + property);
                i--;
            }
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

value is : SHA1withDSA
value is : SHA1withDSA
value is : SHA1withDSA
value is : Software
value is : sun.security.provider.JavaKeyStore$DualFormatJKS
value is : SHA
value is : sun.security.provider.SHA
value is : sun.security.provider.JavaKeyStore$CaseExactJKS
value is : Software
value is : sun.security.provider.DSA$SHA256withDSA

Example 2:




// Java program to demonstrate
// getProperty() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        // Declaring int values
        int i = 10;
  
        try {
            // creating the object of  KeyPairGenerator
            KeyPairGenerator sr = KeyPairGenerator.getInstance("DSA", "SUN");
  
            // getting the Provider of the KeyPairGenerator sr
            // by using method getProvider()
            Provider provider = sr.getProvider();
  
            // getting the mapped value in element
            // using getProperty() method
            System.out.println("Trying to search for unspecified key");
            String property = provider.getProperty("geeks");
  
            // printing the property of specified key
            System.out.println("value is : " + property);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

Trying to search for unspecified key
value is : null


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads