Java.lang.System.setProperty() Method

Advertisements


Description

The java.lang.System.setProperty() method sets the system property indicated by the specified key.

Declaration

Following is the declaration for java.lang.System.setProperty() method

public static String setProperty(String key, String value)

Parameters

  • key -- This is the name of the system property.

  • value -- This is the value of the system property.

Return Value

This method returns the previous value of the system property, or null if it did not have one.

Exception

  • SecurityException -- if a security manager exists and its checkPermission method doesn't allow setting of the specified property.

  • NullPointerException -- if key or value is null.

  • IllegalArgumentException -- if key is empty.

Example

The following example shows the usage of java.lang.System.setProperty() method.

package com.tutorialspoint;

import java.lang.*;

public class SystemDemo {

   public static void main(String[] args) {

     // prints Java Runtime Version before property set
     System.out.print("Previous : ");
     System.out.println(System.getProperty("java.runtime.version" ));
     System.setProperty("java.runtime.version", "Java Runtime 1.6.0");
     
     // prints Java Runtime Version after property set
     System.out.print("New : ");
     System.out.println(System.getProperty("java.runtime.version" ));
   }
} 

Let us compile and run the above program, this will produce the following result:

Previous : 1.6.0_22-b22
New : Java Runtime 1.6.0



Advertisements
Advertisements