目录

static String setProperty(String key, String value)

描述 (Description)

java.lang.System.setProperty()方法设置由指定key指示的系统属性。

声明 (Declaration)

以下是java.lang.System.setProperty()方法的声明

public static String setProperty(String key, String value)

参数 (Parameters)

  • key - 这是系统属性的名称。

  • value - 这是系统属性的值。

返回值 (Return Value)

此方法返回系统属性的先前值,如果没有,则返回null。

异常 (Exception)

  • SecurityException - 如果存在安全管理器且其checkPermission方法不允许设置指定的属性。

  • NullPointerException - 如果key或value为null。

  • IllegalArgumentException - 如果key为空。

例子 (Example)

以下示例显示了java.lang.System.setProperty()方法的用法。

package com.iowiki;
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" ));
   }
} 

让我们编译并运行上面的程序,这将产生以下结果 -

Previous : 1.8.0_65-b17
New : Java Runtime 1.6.0
↑回到顶部↑
WIKI教程 @2018