一个比较恶心的方法是调用Runtime.exec
直接使用win或者unix下查看环境变量的命令
利用process的输入/出流读取

解决方案 »

  1.   

    System.getProperties();
    System.getenv();具体请查看帮助文档。
    getProperty
    public static String getProperty(String key,
                                     String def)Gets the system property indicated by the specified key. 
    First, if there is a security manager, its checkPropertyAccess method is called with the key as its argument. If there is no current set of system properties, a set of system properties is first created and initialized in the same manner as for the getProperties method. 
    Parameters:
    key - the name of the system property.
    def - a default value. 
    Returns:
    the string value of the system property, or the default value if there is no property with that key. 
    Throws: 
    SecurityException - if a security manager exists and its checkPropertyAccess method doesn't allow access to the specified system property. 
    NullPointerException - if key is null. 
    IllegalArgumentException - if key is empty.
    See Also:
    setProperty(java.lang.String, java.lang.String), SecurityManager.checkPropertyAccess(java.lang.String), getProperties()
    getenv
    public static String getenv(String name)Deprecated. The preferred way to extract system-dependent information is the system properties of the java.lang.System.getProperty methods and the corresponding getTypeName 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 external variable that has a string value. Parameters:
    name - of the environment variable 
    Returns:
    the value of the variable, or null if the variable is not defined.
    See Also:
    Boolean.getBoolean(java.lang.String), Integer.getInteger(java.lang.String), Integer.getInteger(java.lang.String, int), Integer.getInteger(java.lang.String, java.lang.Integer), Long.getLong(java.lang.String), Long.getLong(java.lang.String, long), Long.getLong(java.lang.String, java.lang.Long), getProperties(), getProperty(java.lang.String), getProperty(java.lang.String, java.lang.String)
      

  2.   

    Java 中系统环境变量的设置和获取 
    在运行时设置一个环境变量 debug 为 true: java -Ddebug=true YourClass在程序中设置一个环境变量 debug 为 true: System.setProperty( "debug", "true" );获取一个环境变量 debug : String debug = System.getProperty( "debug" );下表中列出了一部分环境变量,这些是 Java 已经定义好的,可以在程序中通过 System.getProperty( "key" ) 来获取,更多的环境变量请参阅 JDK 文档。
    属性名 描 述 
    java.version Java 运行时版本 
    java.home Java 的安装目录 
    java.class.version Java 类格式的版本号 
    java.class.path Java 类的查找路径 
    java.io.tmpdir 默认的临时目录 
    java.compiler Java 所使用的及时编译器 
    java.ext.dirs Java 扩展包的目录 
    os.name 操作系统的名称 
    os.arch 操作系统的体系结构 
    os.version 操作系统的版本 
    file.separator 文件分隔符(Unix 下为'/') 
    path.separator 路径分隔符(Unix 下为':') 
    line.separator 换行符(Unix 下为'\n') 
    user.name 用户帐号名 
    user.home 用户目录 
    user.dir 用户当前的工作目录 
      

  3.   

    一定要先在程序中设置该环境变量吗?不设置不行吗?如:某人手动在系统中设置好了环境变量  env1 = d:\temp
    那么,在JAVA程序中用什么方法来得到env1的变量值。
    (既 env1 是透明的,而env1的值是不知道的)
      

  4.   

    如果你能随便读取,那还有安全性可言?
    java不是号称最安全的?
    所以,不能.因为他没提供这样的接口.
    除非你自己写dll来调用.
    但按您的需求,在手机里运行,就不可能了.
      

  5.   

    在WINDOWS下可以用以下语句直接读取
          Process ObjPrcess = ObjRunTime.exec("cmd /c echo %env1%");
    那么,在UNIX怎么直接读取呢?
      

  6.   

    那么,在UNIX怎么直接读取呢?
      

  7.   

    jni就是我说的“
    因为他没提供这样的接口.
    除非你自己写dll来调用.
    但按您的需求,在手机里运行,就不可能了”还不如你自己说的方法来的简单呢:“在WINDOWS下可以用以下语句直接读取
          Process ObjPrcess = ObjRunTime.exec("cmd /c echo %env1%");
    那么,在UNIX怎么直接读取呢?”