在Java中如何读取Windows的环境变量?!

dimaomao 2003-06-18 03:53:54
在Java中如何读取Windows的环境变量?!
例如:PATH或者TEMP等等……

...全文
255 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
rsldy 2003-09-24
  • 打赏
  • 举报
回复
行了
rsldy 2003-09-24
  • 打赏
  • 举报
回复
不行,打印出来是 :%Path%
dimaomao 2003-06-18
  • 打赏
  • 举报
回复
Java 不支援环境变数, 因为它们是不具可携性的.
不过我们可以在JAVA里面调用WINDOWS的系统命令"Echo %JAVA_HOME%"取得其输出,在经过处理取得字符串返回值.
程序如下:
package test;
import java.io.*;
import java.util.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class Test {
public Test() {
Runtime ObjRunTime = Runtime.getRuntime();
byte[] env = new byte[1000];
try{
Process ObjPrcess = ObjRunTime.exec("cmd /c echo %Path%");
InputStream in = ObjPrcess.getInputStream();
in.read(env);
}catch(Exception e) { e.printStackTrace();}
System.out.println(new String(env).trim());
}
public static void main(String[] args) {
Test test = new Test();
}
}
首先,java.lang.Runtime执行WINDOWS命令.要说明"echo"并不是系统命令所以要在WIN SHELL内执行.
其次,java.lang.Process取得执行结果.
再次,转化为字符串返回.
谢谢诸君关注,来者有分.稍后结贴!
lymkelly 2003-06-18
  • 打赏
  • 举报
回复
关注
dimaomao 2003-06-18
  • 打赏
  • 举报
回复
我需要的是Windows的环境变量,不是system的属性!!
Windows的环境变量例如:"CLASSPATH"、"JAVA_HOME"!!
geyf 2003-06-18
  • 打赏
  • 举报
回复
System.out.println("tmpdir:"+System.getProperty("java.io.tmpdir"));//临时目录

要知道有哪些系统属性可获得,可以看看资料,或者自己写个代码看看:
Properties p=System.getProperties();
Enumeration tttt=p.propertyNames();
while( tttt.hasMoreElements())
System.out.println(tttt.nextElement().toString());
  • 打赏
  • 举报
回复
Class System的Doc
getProperties
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.io.tmpdir Default temp file path
os.name Operating system name
os.arch Operating system architecture
os.version Operating system version
等等
xjx802 2003-06-18
  • 打赏
  • 举报
回复
up

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧