Topic: 郁闷地问:JDK1.3以后如何读取主机上(e.g. UNIX下)环境变量的值 |
Print this page |
1.郁闷地问:JDK1.3以后如何读取主机上(e.g. UNIX下)环境变量的值 | Copy to clipboard |
Posted by: zls Posted on: 2003-12-12 23:31 郁闷地问:JDK1.3以后如何读取主机上(e.g. UNIX下)环境变量的值。System中的getEnv()已经不能用了。 |
2.Re:郁闷地问:JDK1.3以后如何读取主机上(e.g. UNIX下)环境变量的值 [Re: zls] | Copy to clipboard |
Posted by: wlai Posted on: 2003-12-17 11:37 你可以在执行程序时在指令行定义变量, 如 java -DMY_HOME=E:\App\Home MyClass 或以 %XXX% 取出环境变量. 这样便可以在 Java 以系统变量读取 另外一个方法是使用 .properties 檔 |
3.Re:郁闷地问:JDK1.3以后如何读取主机上(e.g. UNIX下)环境变量的值 [Re: zls] | Copy to clipboard |
Posted by: hitaco Posted on: 2003-12-17 12:10 System.getProperty() |
4.Re:郁闷地问:JDK1.3以后如何读取主机上(e.g. UNIX下)环境变量的值 [Re: hitaco] | Copy to clipboard |
Posted by: safe Posted on: 2003-12-17 14:25 How about try this: import java.util.*; |
5.Re:郁闷地问:JDK1.3以后如何读取主机上(e.g. UNIX下)环境变量的值 [Re: zls] | Copy to clipboard |
Posted by: zls Posted on: 2003-12-18 12:50 Thanks all. But sorry,问题问得不是很清楚,郁闷的需求是: 1)UNIX下的.profile(或windows下的autoexec.bat)中已经定义用户自己的一些环境变量,例如:OSS_HOME等。 2)在程序的启动shell里,并不会再加"-DOSS_HOME"等,因为我们希望我们的程序能够透明地直接使用UNIX .profile中已定义的环境变量。因此System.getProperty()是不能被用了。 所以现在的问题是"如果读取UNIX .profile(或windows autoexec.bat)中已定义的环境变量"? |
6.Re:郁闷地问:JDK1.3以后如何读取主机上(e.g. UNIX下)环境变量的值 [Re: zls] | Copy to clipboard |
Posted by: murphys Posted on: 2003-12-22 10:12 zls wrote: 谁说不能用System.getProperty()??? 可以遍历取出所有的properties,代码如下 import java.util.Enumeration; import java.util.Properties; public class GetSystemProfile{ public static void main(String args[]){ Properties properties = System.getProperties(); String s = ""; for(Enumeration enum = properties.propertyNames(); enum.hasMoreElements(); System.out.println(s+"="+properties.getProperty( s ))) s = enum.nextElement()+""; } } |
7.Re:郁闷地问:JDK1.3以后如何读取主机上(e.g. UNIX下)环境变量的值 [Re: zls] | Copy to clipboard |
Posted by: taphoon Posted on: 2003-12-22 12:34 private Properties getEnvVars() { Process p = null; Properties envVars = new Properties(); try { Runtime r = Runtime.getRuntime(); String OS = System.getProperty("os.name").toLowerCase(); // System.out.println(OS); if (OS.indexOf("windows 9") > -1) { p = r.exec( "command.com /c set" ); } else if ( (OS.indexOf("nt") > -1) || (OS.indexOf("windows 2000") > -1 || (OS.indexOf("windows xp") > -1) ) ) { // thanks to JuanFran for the xp fix! p = r.exec( "cmd.exe /c set" ); } else { // our last hope, we assume Unix (thanks to H. Ware for the fix) p = r.exec( "env" ); } BufferedReader br = new BufferedReader ( new InputStreamReader( p.getInputStream() ) ); String line; while( (line = br.readLine()) != null ) { int idx = line.indexOf( '=' ); String key = line.substring( 0, idx ); String value = line.substring( idx+1 ); envVars.setProperty( key, value ); //System.out.println( key + " = " + value ); } } catch (Exception e) { // we do not care here. Just no env vars for the user. Sorry. } return envVars; } |
8.Re:郁闷地问:JDK1.3以后如何读取主机上(e.g. UNIX下)环境变量的值 [Re: zls] | Copy to clipboard |
Posted by: zls Posted on: 2003-12-24 00:56 感觉taphoon这段代码不错,符合需求,我将试试。Thanks all. |
9.Re:郁闷地问:JDK1.3以后如何读取主机上(e.g. UNIX下)环境变量的值 [Re: murphys] | Copy to clipboard |
Posted by: Biubiu Posted on: 2003-12-24 12:56 murphys wrote: 问题是这里面没有他要的东西。 |
10.Re:郁闷地问:JDK1.3以后如何读取主机上(e.g. UNIX下)环境变量的值 [Re: zls] | Copy to clipboard |
Posted by: zls Posted on: 2003-12-27 01:32 呵呵,murphys没有理解我的SRS。 |
11.Re:郁闷地问:JDK1.3以后如何读取主机上(e.g. UNIX下)环境变量的值 [Re: zls] | Copy to clipboard |
Posted by: whisperwind Posted on: 2003-12-28 00:47 windows操作系统在启动一个进程时会将系统的环境变量传入到进程的空间中,所以System.getProperties()是可以获取系统环境变量的,而不是说非得用-Dxxxx=yyyy才能获取xxxx的值。unix我就不太清楚了,不过原理应该和windows差不多吧 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |