getproperty() and getproperties() methods of System Class in Java

The System class in Java has two methods used to read system properties:

  • getProperty: The System class has two different versions of getProperty. Both retrieve the value of the property named in the argument list. The simpler of the two getProperty methods takes a single argument.
  • getProperties: The java.lang.System.getProperties() method determines the current system properties.

Description of methods

  • getProperty(String key) :  java.lang.System.getProperty(String key)  method returns a string containing the value of the property. If the property does not exist, this version of getProperty returns null.
    This is based on key – value pair as mentioned in the table given below.
    Syntax :



    public static String getProperty(String key)
    Parameters :
    key : key whose system property we want
    Returns :
    System property as specified the key
    Null : if there is no property present with that key.

    Implementation :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program illustrating the working of getProperty(String key) method
      
    import java.lang.*;
    import java.util.Properties;
      
    public class NewClass
    {
        public static void main(String[] args)
        {
            // Printing Name of the system property
            System.out.println("user.dir: "+System.getProperty("user.dir"));
      
            // Fetches the property set with 'home' key
            System.out.println("home: "+System.getProperty("home"));
            // Resulting in Null as no property is present
      
            // Printing 'name of Operating System'
            System.out.println("os.name: "+System.getProperty("os.name"));
      
            // Printing 'JAVA Runtime version'
            System.out.println("version: "+System.getProperty("java.runtime.version" ));
      
            // Printing 'name' property
            System.out.println("name: "+System.getProperty("name" ));
            // Resulting in Null as no property is present
        }
    }

    chevron_right

    
    

    Output :

    user.dir: /tmp/hsperfdata_bot
    home: null
    os.name: Linux
    version: 1.8.0_101-b13
    name: null
    
  • getProperty(String key, String definition) : java.lang.System.getProperty(String key, String definition) allows to set the argument definition i.e. one can set a default value for a specific key.
    Syntax :

    public static String getProperty(String key, String def)
    Parameters :
    key : system property 
    def : default value of the key to be specified  
    Returns :
    System Property
    Null : if there is no property present with that key.

    Implementation :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program illustrating the working of 
    // getProperty(String key, String definition) method
    import java.lang.*;
    import java.util.Properties;
      
    public class NewClass
    {
        public static void main(String[] args)
        {
            // use of getProperty(String key, String definition) method
      
            // Here key = "Hello" and System Property = "Geeks"
            System.out.println("Hello property : " 
                            + System.getProperty("Hello", "Geeks"));
      
            // Here key = "Geek" and System Property = "For Geeks"
            System.out.println("System-property :"
                            + System.getProperty("System", "For Geeks"));
          
            // Here key = "Property" and System Property = null
            System.out.println("Property-property :"
                            + System.getProperty("Property"));
      
        }
    }

    chevron_right

    
    

    Output :

    Hello key property : Geeks
    System key property :For Geeks
    Property key property :null
  • getProperties() : java.lang.System.getProperties() fetches the current properties that JVM on your System gets from your Operating System. The current System properties are returned as Properties object for use by the getProperties() method. If no such set of properties is present, a set of system is first created and then initialized.
    One can also modify the existing set of system properties, using System.setProperties() method. There are number of key-value pair in the properties file, some of which are as follows :

    Keys                          Values
    
    -->  os.version              :  OS Version  
    -->  os.name                 :  OS Name
    -->  os.arch                 :  OS Architecture	
    -->  java.compiler           :  Name of the compiler you are using
    -->  java.ext.dirs           :  Extension directory path
    -->  java.library.path       :  Paths to search libraries whenever loading
    -->  path.separator          :  Path separator
    -->  file.separator          :  File separator
    -->  user.dir                :  Current working directory of User
    -->  user.name               :  Account name of User
    -->  java.vm.version         :  JVM implementation version
    -->  java.vm.name            :  JVM implementation name
    -->  java.home               :  Java installation directory
    -->  java.runtime.version    :  JVM version
    

    Syntax :

    public static Properties getProperties()
    Parameters :
    ------
    Returns :
    System properties that JVM gets on your System gets from OS

    Implementation :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program illustrating the working of getProperties() method
      
    import java.lang.*;
    import java.util.Properties;
      
    public class NewClass
    {
        public static void main(String[] args)
        {
      
            /* Use of getProperties() method
                System class refers to the JVM on which you are compling your JAVA code
                getProperty fetches the actual properties
                that JVM on your Sysytem gets from your Operating System
            */
      
            System.out.println("Following are the JVM information of your OS :");
            System.out.println("");
              
            // Property Object
            Properties jvm = System.getProperties();
            jvm.list(System.out);
        }
    }

    chevron_right

    
    

    Output: Click here to see the output

Important Points:

  • java.lang.System.getProperty(String key) : fetches only those properties – values that you will specify using the key(associated to that particluar value that you want).
  • java.lang.System.getProperty(String key, String defination) : helps you to create your own key-value sets that you want.
  • java.lang.System.getProperties() : fetches all the properties – values that the JVM on your System gets from the Operating System.

This article is contributed by Mohit Gupta 🙂. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.