萍聚社区-德国热线-德国实用信息网

 找回密码
 注册

微信登录

微信扫一扫,快速登录

萍聚头条

查看: 656|回复: 1

1-1-9-2-2 用Java编程如何提取环境变量

[复制链接]
发表于 2003-2-6 22:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册 微信登录

x
发信人: gyokuho (Hello_World.), 信区: Java      
标  题: Re: 用Java编程如何提取环境变量
发信站: BBS 水木清华站 (Tue Aug 20 13:38:37 2002)


The preferred way to extract system-dependent information is the system proper
ties of the java.lang.System.getProperty methods and the corresponding getType
Name methods of the Boolean, Integer, and Long primitive types. For example:

     String classPath = System.getProperty("java.class.path",".");

     if (Boolean.getBoolean("myapp.exper.mode"))
         enableExpertCommands();

Gets an environment variable. An environment variable is a system-dependent ex
ternal variable that has a string value.
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
 楼主| 发表于 2003-2-6 22:19 | 显示全部楼层
发信人: gyokuho (Hello_World.), 信区: Java      
标  题: Re: 用Java编程如何提取环境变量
发信站: BBS 水木清华站 (Tue Aug 20 14:03:35 2002)

More about getProperty():

System.getProperty() will retrieve the Java-defined system properties. You can
use the following code to determine the available values:

public static void main( String [] args ) {
     java.util.Properties p = System.getProperties();
     java.util.Enumeration keys = p.keys();
     while( keys.hasMoreElements() ) {
         System.out.println( keys.nextElement() );
     }
}

To retrieve one of these values, such as java.class.path, you can say System.g
etProperty("java.class.path"). If a value you need does not appear automatical
ly in the system properties, you can add it through the -D command line option
:

java -DENV_VARIABLE_NAME=$ENV_VARIABLE_NAME ClassName

You must add a -D entry for each value you need to pass to your program.
On Linux/Unix, if you have a large number of values, you'll want to write a sc
ript that starts up the program. It'll save you a lot of typing!
Alternatively, you can also pipe the return of env to a file. You can then loa
d that file using a Properties object within your program. (You can also exec
env from within your program. However, env does not exist on every platform)



【 在 gyokuho 的大作中提到: 】
: The preferred way to extract system-dependent information is the system pr..
: ties of the java.lang.System.getProperty methods and the corresponding get..
: Name methods of the Boolean, Integer, and Long primitive types. For exampl..
:      String classPath = System.getProperty("java.class.path",".");
:  
:      if (Boolean.getBoolean("myapp.exper.mode"))
:          enableExpertCommands();
:  
: Gets an environment variable. An environment variable is a system-dependen..
: ternal variable that has a string value.
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
您需要登录后才可以回帖 登录 | 注册 微信登录

本版积分规则

手机版|Archiver|AGB|Impressum|Datenschutzerklärung|萍聚社区-德国热线-德国实用信息网 |网站地图

GMT+2, 2024-5-22 00:51 , Processed in 0.052669 second(s), 20 queries , MemCached On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表