我这么写不知对不对, 但结果老是nullpublic class ShowENV{
    public static void main(String[] args) {
    System.out.println(System.getProperty("classpath"));
      
    }
}

解决方案 »

  1.   

    如果你想取得classpath用:
    System.out.println(System.getProperty("java.class.path"));
    你可以到jdk的api文档中取得key,大概得如下:
    Key    Description of Associated Value 
    java.version   Java Runtime Environment version 
    java.vendor   Java Runtime Environment vendor 
    java.vendor.url   Java vendor URL 
    java.home   Java installation directory 
    java.vm.specification.version   Java Virtual Machine specification version 
    java.vm.specification.vendor   Java Virtual Machine specification vendor 
    java.vm.specification.name   Java Virtual Machine specification name 
    java.vm.version   Java Virtual Machine implementation version 
    java.vm.vendor   Java Virtual Machine implementation vendor 
    java.vm.name   Java Virtual Machine implementation name 
    java.specification.version   Java Runtime Environment specification version 
    java.specification.vendor   Java Runtime Environment specification vendor 
    java.specification.name   Java Runtime Environment specification name 
    java.class.version    Java class format version number 
    java.class.path   Java class path 
    java.ext.dirs   Path of extension directory or directories 
    os.name Operating   system name 
    os.arch Operating   system architecture 
    os.version Operating   system version 
    file.separator File   separator ("/" on UNIX) 
    path.separator Path   separator (":" on UNIX) 
    line.separator Line   separator ("\n" on UNIX) 
    user.name   User's account name 
    user.home   User's home directory 
    user.dir   User's current working directory 
      

  2.   

    误会啦! 我是想自己设一个环境变量 比如说 aa=100在程序中拿回我的aa.
      

  3.   

    那你就要先生成一个Properties类型的对象,然后setProperty()不就可以了,
      

  4.   

    我是先在nt或在unix下添加新的环境变量,是手工添加的, 这样为了可以自由的
    进行配置。在程序中拿这变量, 并不是要在程序中设置环境变量。 当然, 假如能提供简短代码 就最好了。 谢谢
      

  5.   

    你理解错了,我要你setProperty()是要你把你想要取得的环境变量以Properties的类型放入System中,这样在以后的getProperty()中,就可以取道你想要的值,一般我们的方法是先把这些property写在一个文件中,初始化的时候把这些property转成Properties类的一个实例,并用setProperty()的方法设为System的property,以后想用的时候,就可以用System.getProperty()的方法取了。
      

  6.   

    通过运行 set 命令读取环境变量    
    Process process = null;
        Properties envVars = new Properties();
        Runtime runtime = Runtime.getRuntime();
        String OS = System.getProperty("os.name").toLowerCase();
        if (OS.indexOf("windows 9") > -1)
        {
          process = runtime.exec( "command.com /c set" );
        }
        else if (OS.indexOf("nt") > -1 || OS.indexOf("windows 2000") > -1)
        {
          process = runtime.exec( "cmd.exe /c set" );
        }
        else if (OS.indexOf("unix") > -1)
        {
          process = runtime.exec( "/bin/env" );
        }
        BufferedReader br = new BufferedReader ( new InputStreamReader( process.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.toUpperCase(), value );
        }
        process.destroy ();
        return envVars;