文选流氓 发表于 2003-2-6 21:18

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

发信人: 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.

文选流氓 发表于 2003-2-6 21: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.
页: [1]
查看完整版本: 1-1-9-2-2 用Java编程如何提取环境变量