System.getEnv()和System.getProperties()的区别
从概念上讲,系统属性和环境变量都是名称与值之间的映射。两种机制都能用来将用户定义的信息传递给 Java 进程。环境变量产生更多的全局效应,因为它们不仅对Java子进程可见,而且对于定义它们的进程的所有子进程都是可见的。在不同的操作系统上,它们的语义有细微的差别,比如,不区分大小写。因为这些原因,环境变量更可能有意料不到的副作用。最好在可能的地方使用系统属性。环境变量应该在需要全局效应的时候使用,或者在外部系统接口要求使用环境变量时使用(比如PATH)。
代码如下:
public static void main(String [] args)
{
Mapm = System.getenv();
for( Iterator it = m.keySet().iterator(); it.hasNext(); )
{
Stringkey = (String ) it.next();
Stringvalue = (String ) m.get(key);
System.out.println(key+":" +value);
}
System.out.println("--------------------------------------" );
Propertiesp = System.getProperties();
for( Iterator it = p.keySet().iterator(); it.hasNext(); )
{
Stringkey = (String ) it.next();
Stringvalue = (String ) p.get(key);
System.out.println(key+":" +value);
}
}
输入如下:
ANT_HOME:D:/program/devel/ant
PROCESSOR_ARCHITECTURE:x86
LOGONSERVER://RJ-WEIJIANJUN
HOMEDRIVE:C:
CATALINA_HOME:D:/program/server/Tomcat5.5
DXSDK_DIR:d:/ProgramFiles/Microsoft DirectX SDK (August 2008)/
VS80COMNTOOLS:C:/ProgramFiles/Microsoft Visual Studio 8/Common7/Tools/
SESSIONNAME:Console
HOMEPATH:/Documents andSettings/Administrator
TMP:C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp
windir:C:/WINDOWS
PROCESSOR_IDENTIFIER:x86 Family6 Model 15 Stepping 13, GenuineIntel
VS90COMNTOOLS:e:/ProgramFiles/Microsoft Visual Studio 9.0/Common7/Tools/
SystemDrive:C:
USERPROFILE:C:/Documents andSettings/Administrator
PATHEXT:.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
CommonProgramFiles:C:/ProgramFiles/Common Files
NUMBER_OF_PROCESSORS:2
ComSpec:C:/WINDOWS/system32/cmd.exe
COMPUTERNAME:RJ-WEIJIANJUN
OS:Windows_NT
USERNAME:Administrator
CLIENTNAME:Console
TEMP:C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp
USERDOMAIN:RJ-WEIJIANJUN
ALLUSERSPROFILE:C:/Documentsand Settings/All Users
lib:C:/Program Files/SQLXML4.0/bin/
PROCESSOR_LEVEL:6
SystemRoot:C:/WINDOWS
ClusterLog:C:/WINDOWS/Cluster/cluster.log
APPDATA:C:/Documents andSettings/Administrator/Application Data
Path:C:/WINDOWS/system32;C:/WINDOWS;C:/WINDOWS/System32/Wbem;C:/ProgramFiles/Microsoft SQL Server/80/Tools/Binn/;C:/Program Files/Microsoft SQLServer/90/DTS/Binn/;C:/Program Files/Microsoft SQLServer/90/Tools/binn/;C:/Program Files/Microsoft SQLServer/90/Tools/Binn/VSShell/Common7/IDE/;C:/Program Files/Microsoft VisualStudio 8/Common7/IDE/PrivateAssemblies/;D:/program/devel/flex_sdk2/bin;D:/program/devel/ant/bin;C:/ProgramFiles/Java/jdk1.6.0_07/bin;%JONAS_ROOT%/bin/nt;d:/program/devel/ant/bin
JAVA_HOME:C:/ProgramFiles/Java/jdk1.6.0_07
FP_NO_HOST_CHECK:NO
PROCESSOR_REVISION:0f0d
ProgramFiles:C:/Program Files
以下是property的输出:
--------------------------------------
java.runtime.name:Java(TM) 2Runtime Environment, Standard Edition
sun.boot.library.path:D:/ProgramFiles/MyEclipse 6.5/jre/bin
java.vm.version:1.5.0_11-b03
java.vm.vendor:Sun MicrosystemsInc.
java.vendor.url:http://java.sun.com/
path.separator:;
java.vm.name:Java HotSpot(TM)Client VM
file.encoding.pkg:sun.io
sun.java.launcher:SUN_STANDARD
user.country:CN
sun.os.patch.level:Service Pack2
java.vm.specification.name:JavaVirtual Machine Specification
user.dir:D:/dev/eclipse/mye65/workspace/jmx
java.runtime.version:1.5.0_11-b03
java.awt.graphicsenv:sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs:D:/ProgramFiles/MyEclipse 6.5/jre/lib/endorsed
os.arch:x86
java.io.tmpdir:C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/
line.separator:
java.vm.specification.vendor:SunMicrosystems Inc.
user.variant:
os.name:Windows 2003
sun.jnu.encoding:GBK
java.library.path:D:/ProgramFiles/MyEclipse6.5/jre/bin;.;C:/WINDOWS/system32;C:/WINDOWS;C:/WINDOWS/system32;C:/WINDOWS;C:/WINDOWS/System32/Wbem;C:/ProgramFiles/Microsoft SQL Server/80/Tools/Binn/;C:/Program Files/Microsoft SQLServer/90/DTS/Binn/;C:/Program Files/Microsoft SQLServer/90/Tools/binn/;C:/Program Files/Microsoft SQLServer/90/Tools/Binn/VSShell/Common7/IDE/;C:/Program Files/Microsoft VisualStudio8/Common7/IDE/PrivateAssemblies/;D:/program/devel/flex_sdk2/bin;D:/program/devel/ant/bin;C:/ProgramFiles/Java/jdk1.6.0_07/bin;%JONAS_ROOT%/bin/nt;d:/program/devel/ant/bin
java.specification.name:JavaPlatform API Specification
java.class.version:49.0
sun.management.compiler:HotSpotClient Compiler
os.version:5.2
user.home:C:/Documents andSettings/Administrator
user.timezone:Asia/Shanghai
java.awt.printerjob:sun.awt.windows.WPrinterJob
file.encoding:GBK
java.specification.version:1.5
java.class.path:D:/dev/eclipse/mye65/workspace/jmx/bin;D:/program/lib/jmx/jmxtools.jar;D:/program/lib/log/commons-logging-1.1.1.jar;D:/program/lib/log/log4j-1.2.15.jar;D:/program/lib/registry/registry.jar
user.name:Administrator
java.vm.specification.version:1.0
java.home:D:/ProgramFiles/MyEclipse 6.5/jre
sun.arch.data.model:32
user.language:zh
java.specification.vendor:SunMicrosystems Inc.
awt.toolkit:sun.awt.windows.WToolkit
java.vm.info:mixed mode
java.version:1.5.0_11
java.ext.dirs:D:/ProgramFiles/MyEclipse 6.5/jre/lib/ext
sun.boot.class.path:D:/ProgramFiles/MyEclipse 6.5/jre/lib/rt.jar;D:/Program Files/MyEclipse6.5/jre/lib/i18n.jar;D:/Program Files/MyEclipse6.5/jre/lib/sunrsasign.jar;D:/Program Files/MyEclipse 6.5/jre/lib/jsse.jar;D:/ProgramFiles/MyEclipse 6.5/jre/lib/jce.jar;D:/Program Files/MyEclipse6.5/jre/lib/charsets.jar;D:/Program Files/MyEclipse 6.5/jre/classes
java.vendor:Sun MicrosystemsInc.
file.separator:/
java.vendor.url.bug:http://java.sun.com/cgi-bin/bugreport.cgi
sun.io.unicode.encoding:UnicodeLittle
sun.cpu.endian:little
sun.desktop:windows
sun.cpu.isalist:pentium_pro+mmxpentium_pro pentium+mmx pentium i486 i386 i86
个人认为比较重要的系统属性:
java.home:Java 安装目录 (比如D:/JavaStudio/jdk15/jre)
java.class.path:Java 类路径
java.vm.version:Java虚拟机版本号(比如:1.5.0-b64)
java.version:Java版本号(比如:1.5.0)
os.name:操作系统的名称(比如:Windows XP)
os.version:操作系统的版本(比如:Windows XP 的版本为5.1)
user.home:用户的主目录 (比如:C:/Documents and Settings/zhangsan)
user.name:当前用户名
user.country:当前用户所属国家
user.dir:用户的当前工作目录
比较重要的环境变量(Windows操作系统):
%COMPUTERNAME% 返回计算机的名称。
%COMSPEC% 返回命令行解释器可执行程序的准确路径。
%WINDIR% 返回操作系统目录的位置。
%OS% 返回操作系统的名称。Windows 2000 将操作系统显示为 Windows_NT。
%PATH% 指定可执行文件的搜索路径。
%USERDOMAIN% 返回包含用户帐户的域的名称。
%USERNAME% 返回当前登录的用户的名称。
下面是一篇在jdk1.4中得到环境变量的方法:
Access userdefined environment variable inside the java program with jdk1.4
In JDK 1.4System.getenv() is deprecated. (In JDK 1.5 it is un-deprected again.) ThegetProperty method is now the correspoding method to get the variables. Forexample: System.getProperty("java.class.path",""). But itdoesn't work for user defined environment variable. This problem can be solvedby using the following code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* Environment class simulates the System.getenv() method which is deprecated
* on java 1.4.2
*
* @author v-josp
*/
public class TestRoot
{
//result of all enviornment variables
private static BufferedReader commandResult;
static
{
String cmd = null;
String os = null;
//getting the OS name
os = System.getProperty("os.name").toLowerCase();
// according to OS set the command to execute
if(os.startsWith("windows"))
{
cmd = "cmd /c SET";
}
else
{
cmd="env";
}
try
{
//execute the command and get the result in the form of InputStream
Process p = Runtime.getRuntime().exec(cmd);
//parse the InputStream data
InputStreamReader isr = new InputStreamReader(p.getInputStream());
commandResult= new BufferedReader(isr);
}
catch (Exception e)
{
System.out.println("OSEnvironment.class error: " + cmd +":" + e);
}
}
/**
* This method is used to get the path of the given enviornment variable. This
* method tries to simulates the System.getenv() which is deprecated on java1.4.2
*
* @param String - name of the environment variable
* @param String - default value
* @return
*/
public static String getenv(String envName,String defaultValue)
{
String line = null;
try
{
while ((line = commandResult.readLine()) != null)
{
if(line.indexOf(envName)>-1)
{
return line.substring(line.indexOf(envName)+envName.length()+1);
}
}
return defaultValue;
}
catch (Exception e)
{
return defaultValue;
}
}
public static voidmain(String args[])
{
System.out.println(TestRoot.getenv("CLASSPATH",""));
}
}
文章转自:http://blog.csdn.net/xiaojianpitt/article/details/2968796