网上很多使用的是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

System.Properties和System.getenv区别的更多相关文章

  1. use Properties objects to maintain its configuration Writing Reading System Properties 维护配置 系统变量

    System Properties (The Java™ Tutorials > Essential Classes > The Platform Environment) https:/ ...

  2. System.getProperty System.getenv 区别 log4j取法

    log4j 可以${}取系统变量相关属性  getProperty Java提供了System类的静态方法getenv()和getProperty()用于返回系统相关的变量与属性,getenv方法返回 ...

  3. (转)内置系统账户:Local system/Network service/Local Service 区别

    最近会转载一些 MSSQL 基础相关的文章. 参考文献: http://www.cnblogs.com/xianspace/archive/2009/04/05/1429835.html 前言 今天在 ...

  4. [转帖]内置系统账户:Local system/Network service/Local Service 区别

    内置系统账户:Local system/Network service/Local Service 区别 学习使用 xp_cmdshell 的时候 发现必须 sqlserver 的服务运行在local ...

  5. Java获取系统环境变量(System Environment Variable)和系统属性(System Properties)以及启动参数的方法

    系统环境变量(System Environment Variable): 在Linux下使用export $ENV=123指定的值.获取的方式如下: Map<String,String> ...

  6. Java System.getProperty vs System.getenv

    转自:https://www.baeldung.com/java-system-get-property-vs-system-getenv 1. Introduction The package ja ...

  7. android Activity类中的finish()、onDestory()和System.exit(0) 三者的区别

    android Activity类中的finish().onDestory()和System.exit(0) 三者的区别 Activity.finish() Call this when your a ...

  8. System.load(String filename)和System.loadLibrary(String libname)的区别

    前言 之前一篇文章在写Native方法的时候,第一个步骤里面有这么一段代码 static { System.load("D:" + File.separator + "H ...

  9. System.nanoTime与System.currentTimeMillis的区别

    平时产生随机数时我们经常拿时间做种子,比如用 System.currentTimeMillis的结果,但是在执行一些循环中使用了System.currentTimeMillis,那么每次的结 果将会差 ...

随机推荐

  1. 在C#中该如何阻止虚方法的覆写

    在开发过程中,我们为了让一个类更有生命力,有时会用virtual来修饰一个方法好让子类来覆写它.但是如果有更新的子子类来覆写时,我们又不想让其影响到上一层的覆写,这时候就要用到new virtual来 ...

  2. HashMap实现原理及源码分析

    哈希表(hash table)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希表,而HashMap的实现原理也常常出 ...

  3. Hibernate入门案例及增删改查

    一.Hibernate入门案例剖析: ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private I ...

  4. Smokeping -- 监控网络质量

    1.下载fping.echoping.smokeping 链接:http://pan.baidu.com/s/1pL4HLYb 密码:fxe2 2.安装依赖包 yum install -y perl ...

  5. Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案

    的最佳方案 标签: Android屏幕旋转AsyncTaskProgressDialog 2014-07-19 09:25 39227人阅读 评论(46) 收藏 举报 分类: [android 进阶之 ...

  6. 脑成像数据分析:Python工具包

    来源:SealHuang 脑成像技术已经成为认知科学和心理学研究领域中一种重要的研究手段,帮助研究者不断深入发掘我们脑中的秘密.伴随着研究的不断深入,各式各样的指标参数和分析方法也不断推陈出新,以迅雷 ...

  7. Java开发环境的搭建以及使用eclipse从头一步步创建java项目

    一.java 开发环境的搭建 这里主要说的是在windows 环境下怎么配置环境. 1.首先安装JDK java的sdk简称JDK ,去其官方网站下载最近的JDK即可..http://www.orac ...

  8. C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 实现缓存预热

    因为大型应用系统可能有几十个子系统,为了减轻数据库频繁读写压力.提高系统的运行速度.反映速度,大型应用系统都需要采用缓存机制提高运行效率.Redis 缓存预热实现将来大家很多基础数据都可以缓存获取,不 ...

  9. jS字符串大小写转换实现方式

    toLocaleUpperCase 方法:将字符转换为大写 stringVar.tolocaleUpperCase( ) 必选的 stringVar 引用是一个 String 对象,值或文字. //转 ...

  10. Word2Vec 使用总结

    word2vec 是google 推出的做词嵌入(word embedding)的开源工具. 简单的说,它在给定的语料库上训练一个模型,然后会输出所有出现在语料库上的单词的向量表示,这个向量称为&qu ...