The role and use of System.getProperty() in java

When looking at some code recently, System.getProperty(), System.load(), System.loadLibrary(), etc. are used in many places

System can have standard input, standard output, and error output streams; access to externally defined properties and environment variables; methods to load files and libraries; System properties, the return value is a Properties;
System.load(String filename) is equivalent to: System.getProperties().load(String filename) Their function is to use the specified filename from the local file system as a dynamic library Load the code file.

System.setProperties(Properties propes): Set the system properties as the Properties parameter;
System.setProperties(String key, String value) is equivalent to System.getProperties().setProperties(String key, String value): Set the system properties indicated by the specified key

JDK API

http://tool.oschina.net/apidocs/apidoc?api=jdk-zh  

static void setProperties(Properties props)
          Set the system property to Propertiesparameter .
static String setProperty(String key, String value)
          Sets the system property indicated by the specified key.

 

 

static Properties getProperties()
          Determines the current system properties.
static String getProperty(String key)
          Gets the system property indicated by the specified key.
static String getProperty(String key, String def)
          Get the system property described with the specified key.

1、setProperties

public static void setProperties(Properties props)
Set the system property to Properties parameter .

First, if there is a security manager, its checkPropertiesAccessmethods . This may result in a security exception.

The parameter is a collection of current system properties used by the getProperty(String)method . If the argument is null, the current collection of system properties is ignored.

 


Parameters: props - New system property. Throws: SecurityException - if a security manager exists and its checkPropertiesAccess methods do not allow access to system properties. See also: getProperties() , Properties , SecurityException , SecurityManager.checkPropertiesAccess()

2、System.getProperties

 

public static Properties getProperties()

 

Determines the current system properties.

First, if there is a security manager, its checkPropertiesAccessmethods . This may result in a security exception.

Returns the current collection of system properties used by the getProperty(String)method as an Propertiesobject. If there is no current system property set, create and initialize a system property set first. This collection of system properties always contains values ​​for the following keys:

Description of key-related values
java.version Java Runtime Environment version
java.vendor Java Runtime Environment Provider
java.vendor.url URL of the Java vendor
java.home Java installation directory
java.vm.specification.version Java Virtual Machine Specification Version
java.vm.specification.vendor Java Virtual Machine Specification Vendor
java.vm.specification.name Java Virtual Machine canonical name
java.vm.version Java Virtual Machine Implementation Version
java.vm.vendor Java Virtual Machine Implementation Vendors
java.vm.name Java Virtual Machine Implementation Name
java.specification.version Java Runtime Environment Specification Version
java.specification.vendor Java Runtime Environment Specification Vendor
java.specification.name Java Runtime Environment canonical name
java.class.version Java class format version number
java.class.path Java classpath
java.library.path A list of paths searched when loading libraries
java.io.tmpdir Default temporary file path
java.compiler The name of the JIT compiler to use
java.ext.dirs Path to one or more extension directories
os.name the name of the operating system
os.arch The architecture of the operating system
os.version OS version
file.separator file separator ("/" on UNIX systems)
path.separator Path separator (":" on UNIX systems)
line.separator line separator ("/n" on UNIX systems)
user.name User's account name
user.home user's home directory
user.dir the user's current working directory

Multiple paths in a system property value are separated by the platform's path separator.

Note that the security getPropertiesmanager may choose to allow the getProperty(String)operation even if it does not allow it.

 


Returns: system property Throws SecurityException : - If a security manager exists and its checkPropertiesAccess methods do not allow access to system properties. See also: setProperties(java.util.Properties) , SecurityException , SecurityManager.checkPropertiesAccess() , Properties
Java code
  1. public class  TestSystemGetSet {   
  2.     static{  
  3.         System.setProperty( "DB" "mysql" ); //Can be used as a global variable anywhere  
  4.     }  
  5.     publicstaticvoid main(String[] args) {    
  6.         System.out.println(System.getProperty("os.version"));  
  7.         System.out.println(System.getProperty("java.library.path"));  
  8.         System.out.println(System.getProperty("DB"));  
  9.     }  
  10. }  
输出结果:
10.0
C:\Java\jdk1.8.0_121\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Java/jre8/bin/server;C:/Java/jre8/bin;C:/Java/jre8/lib/amd64;C:\Program Files\IBM\WebSphere MQ\java\lib;C:\Program Files\IBM\WebSphere MQ\java\lib64;E:\app\Administrator\product\11.2.0\dbhome_2\bin;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\MySQL\MySQL Server 5.1\bin;D:/maven\bin;C:\Program Files (x86)\Rational\common;C:\Program Files\IBM\WebSphere MQ\bin64;C:\Program Files\IBM\WebSphere MQ\bin;C:\Program Files\IBM\WebSphere MQ\tools\c\samples\bin;C:\Java\jdk1.8.0_121\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;D:\maven\bin;E:\soft\apache-ant-1.10.1\bin;D:\erl9.2\bin;;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;E:\tool\eclipse;;.
mysql
 

最近在看一些代码时,很多地方都用到了System.getProperty()、System.load()、System.loadLibrary()等等

System可以有对标准输入,标准输出,错误输出流;对外部定义的属性和环境变量的访问;加载文件和库的方法;还有快速复制数组的一部分的实用方法System.getProperties()可以确定当前的系统属性,返回值是一个Properties;
System.load(String filename)等同于:System.getProperties().load(String filename)它们的作用是可以从作为动态库的本地文件系统中以指定的文件名加载代码文件。

System.setProperties(Properties propes):将系统属性设置为Properties参数;
System.setProperties(String key,String value)等同于System.getProperties().setProperties(String key,String value):设置指定键指示的系统属性

JDK API

http://tool.oschina.net/apidocs/apidoc?api=jdk-zh  

static void setProperties(Properties props)
          将系统属性设置为 Properties 参数。
static String setProperty(String key, String value)
          设置指定键指示的系统属性。

 

 

static Properties getProperties()
          确定当前的系统属性。
static String getProperty(String key)
          获取指定键指示的系统属性。
static String getProperty(String key, String def)
          获取用指定键描述的系统属性。

1、setProperties

public static void setProperties(Properties props)
Set the system property to Properties parameter .

First, if there is a security manager, its checkPropertiesAccessmethods . This may result in a security exception.

The parameter is a collection of current system properties used by the getProperty(String)method . If the argument is null, the current collection of system properties is ignored.

 


Parameters: props - New system property. Throws: SecurityException - if a security manager exists and its checkPropertiesAccess methods do not allow access to system properties. See also: getProperties() , Properties , SecurityException , SecurityManager.checkPropertiesAccess()

2、System.getProperties

 

public static Properties getProperties()

 

Determines the current system properties.

First, if there is a security manager, its checkPropertiesAccessmethods . This may result in a security exception.

Returns the current collection of system properties used by the getProperty(String)method as an Propertiesobject. If there is no current system property set, create and initialize a system property set first. This collection of system properties always contains values ​​for the following keys:

Description of key-related values
java.version Java Runtime Environment version
java.vendor Java Runtime Environment Provider
java.vendor.url URL of the Java vendor
java.home Java installation directory
java.vm.specification.version Java Virtual Machine Specification Version
java.vm.specification.vendor Java Virtual Machine Specification Vendor
java.vm.specification.name Java Virtual Machine canonical name
java.vm.version Java Virtual Machine Implementation Version
java.vm.vendor Java Virtual Machine Implementation Vendors
java.vm.name Java Virtual Machine Implementation Name
java.specification.version Java Runtime Environment Specification Version
java.specification.vendor Java Runtime Environment Specification Vendor
java.specification.name Java Runtime Environment canonical name
java.class.version Java class format version number
java.class.path Java classpath
java.library.path A list of paths searched when loading libraries
java.io.tmpdir Default temporary file path
java.compiler The name of the JIT compiler to use
java.ext.dirs Path to one or more extension directories
os.name the name of the operating system
os.arch The architecture of the operating system
os.version OS version
file.separator file separator ("/" on UNIX systems)
path.separator Path separator (":" on UNIX systems)
line.separator line separator ("/n" on UNIX systems)
user.name User's account name
user.home user's home directory
user.dir the user's current working directory

Multiple paths in a system property value are separated by the platform's path separator.

Note that the security getPropertiesmanager may choose to allow the getProperty(String)operation even if it does not allow it.

 


Returns: system property Throws SecurityException : - If a security manager exists and its checkPropertiesAccess methods do not allow access to system properties. See also: setProperties(java.util.Properties) , SecurityException , SecurityManager.checkPropertiesAccess() , Properties
Java code
  1. public class  TestSystemGetSet {   
  2.     static{  
  3.         System.setProperty( "DB" "mysql" ); //Can be used as a global variable anywhere  
  4.     }  
  5.     publicstaticvoid main(String[] args) {    
  6.         System.out.println(System.getProperty("os.version"));  
  7.         System.out.println(System.getProperty("java.library.path"));  
  8.         System.out.println(System.getProperty("DB"));  
  9.     }  
  10. }  
输出结果:
10.0
C:\Java\jdk1.8.0_121\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Java/jre8/bin/server;C:/Java/jre8/bin;C:/Java/jre8/lib/amd64;C:\Program Files\IBM\WebSphere MQ\java\lib;C:\Program Files\IBM\WebSphere MQ\java\lib64;E:\app\Administrator\product\11.2.0\dbhome_2\bin;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\MySQL\MySQL Server 5.1\bin;D:/maven\bin;C:\Program Files (x86)\Rational\common;C:\Program Files\IBM\WebSphere MQ\bin64;C:\Program Files\IBM\WebSphere MQ\bin;C:\Program Files\IBM\WebSphere MQ\tools\c\samples\bin;C:\Java\jdk1.8.0_121\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;D:\maven\bin;E:\soft\apache-ant-1.10.1\bin;D:\erl9.2\bin;;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;E:\tool\eclipse;;.
mysql
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326095293&siteId=291194637