2011-01-31 74 views

回答

29

只需通過調用System.getenv(...)

System.getenv("ProgramFiles"); 

注意到它只會在Windows環境下工作,當然:-)

2

使用System.getenv()方法:

public class EnvironmentVariableExample { 

    public static void main(String[] args) { 
     System.out.println(System.getenv("ProgramFiles")); 
     System.out.println(System.getenv("MadeUpEnvVar")); 
    } 
} 

如果該變量不存在,它會簡單地返回null。

4
System.getenv("%programfiles% (x86)"); 

用於64位PC上的32位文件夾。

另外,它適用於Windows Vista及更新版本的任何語言。實際上,調用發佈的任何一個響應都適用於任何語言安裝。

+0

在使用jdk 8的Windows 7中不起作用 – privatejava 2016-06-06 16:14:16

0

對於32位應用:

System.out.println(System.getenv("ProgramFiles(X86)")); 

對於64位應用:

System.out.println(System.getenv("ProgramFiles"));