java中获取系统属性以及环境变量
System.getEnv()和System.getProperties()的差别
从概念上讲,系统属性 和环境变量 都是名称与值之间的映射。两种机制都能用来将用户定义的信息传递给 Java 进程。环境变量产生很多其它的全局效应,由于它们不仅对Java 子进程可见,并且对于定义它们的进程的全部子进程都是可见的。在不同的操作系统上,它们的语义有细微的区别,比方,不区分大写和小写。由于这些原因,环境变量更可能有意料不到的副作用。最好在可能的地方使用系统属性。环境变量应该在须要全局效应的时候使用,或者在外部系统接口要求使用环境变量时使用(比方 PATH)。
代码例如以下:
public static void main(String [] args)
{
Map
m = System.getenv();
for
( Iterator it = m.keySet().iterator(); it.hasNext(); )
{
String
key = (String ) it.next();
String
value = (String ) m.get(key);
System.out.println(key
+":" +value);
}
System.out.println(
"--------------------------------------" );
Properties
p = System.getProperties();
for
( Iterator it = p.keySet().iterator(); it.hasNext(); )
{
String
key = (String ) it.next();
String
value = (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:/Program
Files/Microsoft DirectX SDK (August 2008)/
VS80COMNTOOLS:C:/Program
Files/Microsoft Visual Studio 8/Common7/Tools/
SESSIONNAME:Console
HOMEPATH:/Documents and
Settings/Administrator
TMP:C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp
windir:C:/WINDOWS
PROCESSOR_IDENTIFIER:x86 Family
6 Model 15 Stepping 13, GenuineIntel
VS90COMNTOOLS:e:/Program
Files/Microsoft Visual Studio 9.0/Common7/Tools/
SystemDrive:C:
USERPROFILE:C:/Documents and
Settings/Administrator
PATHEXT:.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
CommonProgramFiles:C:/Program
Files/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:/Documents
and Settings/All Users
lib:C:/Program Files/SQLXML
4.0/bin/
PROCESSOR_LEVEL:6
SystemRoot:C:/WINDOWS
ClusterLog:C:/WINDOWS/Cluster/cluster.log
APPDATA:C:/Documents and
Settings/Administrator/Application Data
Path:C:/WINDOWS/system32;C:/WINDOWS;C:/WINDOWS/System32/Wbem;C:/Program
Files/Microsoft SQL Server/80/Tools/Binn/;C:/Program Files/Microsoft SQL
Server/90/DTS/Binn/;C:/Program Files/Microsoft SQL
Server/90/Tools/binn/;C:/Program Files/Microsoft SQL
Server/90/Tools/Binn/VSShell/Common7/IDE/;C:/Program Files/Microsoft Visual
Studio 8/Common7/IDE/PrivateAssemblies/;D:/program/devel/flex_sdk2/bin;D:/program/devel/ant/bin;C:/Program
Files/Java/jdk1.6.0_07/bin;%JONAS_ROOT%/bin/nt;d:/program/devel/ant/bin
JAVA_HOME:C:/Program
Files/Java/jdk1.6.0_07
FP_NO_HOST_CHECK:NO
PROCESSOR_REVISION:0f0d
ProgramFiles:C:/Program Files
下面是property的输出:
--------------------------------------
java.runtime.name:Java(TM) 2
Runtime Environment, Standard Edition
sun.boot.library.path:D:/Program
Files/MyEclipse 6.5/jre/bin
java.vm.version:1.5.0_11-b03
java.vm.vendor:Sun Microsystems
Inc.
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 Pack
2
java.vm.specification.name:Java
Virtual 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:/Program
Files/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:Sun
Microsystems Inc.
user.variant:
os.name:Windows 2003
sun.jnu.encoding:GBK
java.library.path:D:/Program
Files/MyEclipse
6.5/jre/bin;.;C:/WINDOWS/system32;C:/WINDOWS;C:/WINDOWS/system32;C:/WINDOWS;C:/WINDOWS/System32/Wbem;C:/Program
Files/Microsoft SQL Server/80/Tools/Binn/;C:/Program Files/Microsoft SQL
Server/90/DTS/Binn/;C:/Program Files/Microsoft SQL
Server/90/Tools/binn/;C:/Program Files/Microsoft SQL
Server/90/Tools/Binn/VSShell/Common7/IDE/;C:/Program Files/Microsoft Visual
Studio
8/Common7/IDE/PrivateAssemblies/;D:/program/devel/flex_sdk2/bin;D:/program/devel/ant/bin;C:/Program
Files/Java/jdk1.6.0_07/bin;%JONAS_ROOT%/bin/nt;d:/program/devel/ant/bin
java.specification.name:Java
Platform API Specification
java.class.version:49.0
sun.management.compiler:HotSpot
Client Compiler
os.version:5.2
user.home:C:/Documents and
Settings/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:/Program
Files/MyEclipse 6.5/jre
sun.arch.data.model:32
user.language:zh
java.specification.vendor:Sun
Microsystems Inc.
awt.toolkit:sun.awt.windows.WToolkit
java.vm.info:mixed mode
java.version:1.5.0_11
java.ext.dirs:D:/Program
Files/MyEclipse 6.5/jre/lib/ext
sun.boot.class.path:D:/Program
Files/MyEclipse 6.5/jre/lib/rt.jar;D:/Program Files/MyEclipse
6.5/jre/lib/i18n.jar;D:/Program Files/MyEclipse
6.5/jre/lib/sunrsasign.jar;D:/Program Files/MyEclipse 6.5/jre/lib/jsse.jar;D:/Program
Files/MyEclipse 6.5/jre/lib/jce.jar;D:/Program Files/MyEclipse
6.5/jre/lib/charsets.jar;D:/Program Files/MyEclipse 6.5/jre/classes
java.vendor:Sun Microsystems
Inc.
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+mmx
pentium_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:当前username
user.country:当前用户所属国家
user.dir:用户的当前工作文件夹
比較重要的环境变量(Windows操作系统):
%COMPUTERNAME% 返回计算机的名称。
%COMSPEC% 返回命令行解释器可运行程序的准确路径。
%WINDIR% 返回操作系统文件夹的位置。
%OS% 返回操作系统的名称。Windows 2000 将操作系统显示为 Windows_NT。
%PATH% 指定可运行文件的搜索路径。
%USERDOMAIN% 返回包括用户帐户的域的名称。
%USERNAME% 返回当前登录的用户的名称。
以下是一篇在jdk1.4中得到环境变量的方法:
Access user
defined environment variable inside the java program with jdk1.4
In JDK 1.4
System.getenv() is deprecated. (In JDK 1.5 it is un-deprected again.) The
getProperty method is now the correspoding method to get the variables. For
example: System.getProperty("java.class.path",""). But it
doesn't work for user defined environment variable. This problem can be solved
by 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 java
1.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 void
main(String args[])
{
System.out.println(TestRoot.getenv("CLASSPATH",""));
}
}
java中获取系统属性以及环境变量的更多相关文章
- Java获取系统属性及环境变量
当程序中需要使用与操作系统相关的变量(例如:文件分隔符.换行符)时,Java提供了System类的静态方法getenv()和getProperty()用于返回系统相关的变量与属性,getenv方法返回 ...
- Spring中如何向 Bean注入系统属性或环境变量
[转自] http://unmi.cc/spring-injection-system-properties-env/ 在 Spring 中为 javabean 注入属性文件中的属性值一般人都知道的, ...
- java中获取系统的当前时间
转自:http://www.cnblogs.com/Matrix54/archive/2012/05/01/2478158.html 一. 获取当前系统时间和日期并格式化输出: import java ...
- java中获取系统变量
System.getProperty("java.version") 用来获取系统变量.getProperty()这个方法是获取指定键指示的系统属性的.以下是通过System.ge ...
- JVM系统属性 OS环境变量 JVM启动参数
JVM系统属性(System Properties) 1.不支持通过文件查看和设置系统属性 2.可以通过JDK自带的工具jvisulavm.exe查看 3.可以在Java程序中使用API来查看系统属性 ...
- java之重装系统重新配置环境变量 jdk、eclipse、idea、Oracle、svn、gitlab等环境变量的安装
前言:由于公司电脑进行统一版本升级,需要重装系统(只对C盘做升级),记录一下踩过的坑! 首先理一下思路,看那些东西需要做: 1.jdk及其环境变量 2.eclipse(文件夹版的需要运行项目进行测试) ...
- java总结:Java中获取系统时间(年、月、日)以及下拉菜单默认选择系统年、月、日的方法
<!-- 获取系统当前的年.月.日 --> <%@ page import="java.util.*"%> <% Calendar calendar= ...
- maven中可以直接引用的java系统属性和环境变量属性
一.查看命令: 1 mvn help :system 二.引用 在pom文件中通过 ${变量名}来引用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
- Java中字段、属性、成员变量、局部变量、实例变量、静态变量、类变量、常量
首先看个例子: package zm.demo; public class Demo { private int Id;//成员变量(字段).实例变量(表示该Id变量既属于成员变量又属于实例变量) p ...
随机推荐
- sass用法
可能刚开始我们学习前端的时候都习惯用html+css.来做网页,但是我们发现css有很多重复的代码或者是你要改里面的图片或者文字还有去诶个的找很麻烦,所以我们就用sass来简化它. 首先我们需要安装一 ...
- linux自定义开机启动服务
转 http://www.cnblogs.com/jimeper/archive/2013/03/12/2955687.html 手工创建服务 1.在/etc/rc.d/init.d目录下创建shel ...
- Android 安全概述
1. 保密: 信息.文档加密.解密 2. 鉴别.认证:就像确认与我们打电话的确实是XXX,而不是骗子 3. 完整性:信息不能被篡改, 4. 不可否认性:确定信息有who产生,并且将来不能否认
- alter system register
alter system register的用法 1 Static Registration via set the listener.ora2 Dynamic Instance Registrati ...
- java入门(与C++的不同之处)封装篇
初学java,总是想将它与之前的C++做比较,看了慕客网的java入门视频,一直觉得在面向对象方面,它和C++有太多相同的地方,结果今天学到了两点不同之处,现在将它记录下来: 1. java的访问修 ...
- 有关collection中的一些数据结构
Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同的元素 ...
- js performance
http://hqman.me/2012/js-module.html http://www.cnblogs.com/snandy/archive/2012/06/06/2536969.html ht ...
- SQL Server ->> 深入探讨SQL Server 2016新特性之 --- Temporal Table(历史表)
原文:SQL Server ->> 深入探讨SQL Server 2016新特性之 --- Temporal Table(历史表) 作为SQL Server 2016(CTP3.x)的另一 ...
- Android 一个页面上下两个ListView的页面显示
Android 一个页面上下两个ListView,当上面的ListView过长时,下面的List基本没有了滑动空间,查阅网上资料,解决办法基本是采用ScrollView做页面滑动,notifyData ...
- Android应用--新浪微博客户端新特性滚动视图和启动界面实现
新浪微博客户端新特性滚动视图和启动界面实现 2013年8月20日新浪微博客户端开发之启动界面实现 前言: 使用过新浪微博客户端的童鞋都清楚,客户端每一次升级之后第一次启动界面就会有新特性的介绍,用户通 ...