文章出處

網上很多使用的是getProperties。說獲得系統變量,但是其實不正確。getProperties中所謂的"system properties"其實是指"java system",而非"operation system",概念完全不同,使用getProperties獲得的其實是虛擬機的變量形如: -Djavaxxxx。


getenv方法才是真正的獲得系統環境變量,比如Path之類。其方法命名方式有違Sun命名規范其實。


意思就是說:希望使用Java的系統變量替代操作系統的變量獲取,如果你想訪問某個系統的環境變量(operation system properties),請把他重新定義個名字,傳給Java的JVM變量(jvm system properties)。要獲得系統的環境變量,請使用:


getenv()方法。


這才最正確。


getProperties()
Determines the current system properties.


public static Properties getProperties()
 
Determines the current system properties.
 
First, if there is a security manager, its checkPropertiesAccess method is called with no arguments. This may result in a security exception.
 
The current set of system properties for use by the getProperty(String) method is returned as a Properties object. If there is no current set of system properties, a set of system properties is first created and initialized. This set of system properties always includes values for the following keys:
Key Description of Associated Value
java.version Java Runtime Environment version
java.vendor Java Runtime Environment vendor
java.vendor.url Java vendor URL
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 specification name
java.vm.version Java Virtual Machine implementation version
java.vm.vendor Java Virtual Machine implementation vendor
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 specification name
java.class.version Java class format version number
java.class.path Java class path
java.library.path List of paths to search when loading libraries
java.io.tmpdir Default temp file path
java.compiler Name of JIT compiler to use
java.ext.dirs Path of extension directory or directories
os.name Operating system name
os.arch Operating system architecture
os.version Operating system version
file.separator File separator ("/" on UNIX)
path.separator Path separator (":" on UNIX)
line.separator Line separator ("\n" on UNIX)
user.name User's account name
user.home User's home directory
user.dir User's current working directory
 
Multiple paths in a system property value are separated by the path separator character of the platform.
 
Note that even if the security manager does not permit the getProperties operation, it may choose to permit the getProperty(String) operation.
 
Returns:
    the system properties
Throws:
    SecurityException - if a security manager exists and its checkPropertiesAccess method doesn't allow access to the system properties.
See Also:
    setProperties(java.util.Properties), SecurityException, SecurityManager.checkPropertiesAccess(), Properties


http://archlord.blog.hexun.com/6949672_d.html
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):設置指定鍵指示的系統屬性


對于在程序中如果我們想得到一個資源文件中對應的鍵值對的內容,可以有兩種方法:
1)使用Properties的load方法,將這個文件先加載進來,之后使用getProperty方法將對應鍵的值得到,比如:
System.getProperties().load("System.Properties.txt");先加載System.Properties.txt文件
System.getProperties().getProperty("DBType");后將文件中鍵為DBType的值得到。
2)使用第一種方法鍵對應的值得靈活性比較大。還有一種方法是將不從文件中得到鍵對應的值。在程序中去設一個屬性,比如:
System.getProperties().setProperty("DBType","SQLServer");先設置一個鍵位DBType的屬性
System.getProperties().getProperty("DBType");后通過getProperty方法得到DBType的值。


另外使用Properties.getProperty方法的參數也可以使用系統的一些環境變量,列表如下:
Key                     Meaning
-------------------     ------------------------------
"file.separator"        File separator (e.g., "/")
"java.class.path"       Java classpath
"java.class.version"    Java class version number
"java.home"             Java installation directory
"java.vendor"           Java vendor-specific string
"java.vendor.url"       Java vendor URL
"java.version"          Java version number
"line.separator"        Line separator
"os.arch"               Operating system architecture
"os.name"               Operating system name
"path.separator"        Path separator (e.g., ":")
"user.dir"              User's current working directory
"user.home"             User home directory
"user.name"             User account name
使用其中的key可以得到一些屬性,供我們在程序中使用
備注:
Microsoft VM是WIN32操作環境中的虛擬機,VM一般安裝在大多數操作系統下,也包含在多數IE中。
Microsoft VM存在漏洞允許攻擊者對user.dir屬性進行訪問。user.dir屬性包含當前應用程序的工作目錄信息,也包含用戶名信息,利用這個漏洞可以獲得當前用戶名稱。
可以利用WEB頁和HTML形式郵件來觸發。


System.getenv()
根據JDK 7中的描述


Returns an unmodifiable string map view of the current system environment.


public static Map<String,String> getenv()
 
Returns an unmodifiable string map view of the current system environment. The environment is a system-dependent mapping from names to values which is passed from parent to child processes.
 
If the system does not support environment variables, an empty map is returned.
 
The returned map will never contain null keys or values. Attempting to query the presence of a null key or value will throw a NullPointerException. Attempting to query the presence of a key or value which is not of type String will throw a ClassCastException.
 
The returned map and its collection views may not obey the general contract of the Object.equals(java.lang.Object) and Object.hashCode() methods.
 
The returned map is typically case-sensitive on all platforms.
 
If a security manager exists, its checkPermission method is called with a RuntimePermission("getenv.*") permission. This may result in a SecurityException being thrown.
 
When passing information to a Java subprocess, system properties are generally preferred over environment variables.
 
Returns:
    the environment as a map of variable names to values
Throws:
    SecurityException - if a security manager exists and its checkPermission method doesn't allow access to the process environment
Since:
    1.5
See Also:
    getenv(String), ProcessBuilder.environment()

來源:http://blog.csdn.net/lanwenbing/article/details/40780971


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()