Reputation: 11053
How can I get the current computer's "Program Files" path with Java?
Upvotes: 27
Views: 26106
Reputation: 485
For 32 bit use:
System.out.println(System.getenv("ProgramFiles(X86)"));
For 64 bit use:
System.out.println(System.getenv("ProgramFiles"));
Upvotes: 4
Reputation: 57
System.getenv("%programfiles% (x86)");
for the 32-bit folder on 64-bit PC's.
Also, it works on any language in Windows Vista and newer. Calling either of the posted responses will work on any language installation, in fact.
Upvotes: 4
Reputation: 7438
Use the System.getenv() method:
public class EnvironmentVariableExample {
public static void main(String[] args) {
System.out.println(System.getenv("ProgramFiles"));
System.out.println(System.getenv("MadeUpEnvVar"));
}
}
If the variable doesn't exist, it will simply return null.
Upvotes: 2
Reputation: 22292
Simply by calling System.getenv(...)
System.getenv("ProgramFiles");
Notice it will only work in Windows environments, of course :-)
Upvotes: 40