Java System setProperty() Method



Description

The Java 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: Clearing User Directory and Setting New Path

The following example shows the usage of Java System setProperty() method. In this example, we've cleared the current user.dir system property using System.clearProperty() method. We've used the returned value of clearProperty() to print the previous user.dir property value. Using setProperty() method, a new path is set as user.dir and using getProperty(), this updated property is retrieved and printed.

package com.tutorialspoint;

public class SystemDemo {

   public static void main(String[] args) {

      // returns the previous string value of the system property
      String userDir = System.clearProperty("user.dir");
      System.out.println("Previous user.dir = " + userDir);

      // sets the new property
      System.setProperty("user.dir", "C:/tutorialspoint/java");

      // gets the system property after changes done by setProperty
      System.out.println("Current user.dir = " + System.getProperty("user.dir"));
   }
} 

Output

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

Previous user.dir = C:\Users\Tutorialspoint\eclipse-workspace\Tutorialspoint
Current user.dir = C:/tutorialspoint/java

Example: Clearing Java Class Path and Setting New Path

The following example shows the usage of Java System setProperty() method. In this example, we've cleared the current java.class.path system property using System.clearProperty() method. We've used the returned value of clearProperty() to print the previous java.class.path property value. Using setProperty() method, a new path is set as java.class.path and using getProperty(), this updated property is retrieved and printed.

package com.tutorialspoint;

public class SystemDemo {

   public static void main(String[] args) {

      // returns the previous string value of the system property
      String classPath = System.clearProperty("java.class.path");
      System.out.println("Previous Class Path = " + classPath);

      // sets the new property
      System.setProperty("java.class.path", "C:/tutorialspoint/java");

      // gets the system property after changes done by setProperty
      System.out.println("Current Class Path = " + System.getProperty("java.class.path"));
   }
} 

Output

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

Previous Class Path = C:\Users\Tutorialspoint\eclipse-workspace\Tutorialspoint\bin;C:\Users\Tutorialspoint\eclipse-workspace\Tutorialspoint\lib\jmh-core-1.37.jar;C:\Users\Tutorialspoint\eclipse-workspace\Tutorialspoint\lib\jmh-generator-annprocess-1.37.jar;C:\Users\Tutorialspoint\eclipse-workspace\Tutorialspoint\lib
Current Class Path = C:/tutorialspoint/java

Example: Clearing Non-Existing Property and Setting New Value

The following example shows the usage of Java System setProperty() method. In this example, we've cleared the current tmp.dir system property using System.clearProperty() method. We've used the returned value of clearProperty() to print the previous tmp.dir property value as null. Using setProperty() method, a new path is set as tmp.dir and using getProperty(), this updated property is retrieved and printed.

package com.tutorialspoint;

public class SystemDemo {

   public static void main(String[] args) {

      // returns the previous string value of the system property
      String tmpDir = System.clearProperty("tmp.dir");
      System.out.println("Previous Temporary Dir = " + tmpDir);

      // sets the new property
      System.setProperty("tmp.dir", "C:/tutorialspoint/java");

      // gets the system property after changes done by setProperty
      System.out.println("Current Temporary Dir = " + System.getProperty("tmp.dir"));
   }
}  

Output

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

Previous Temporary Dir = null
Current Temporary Dir = C:/tutorialspoint/java
java_lang_system.htm
Advertisements