Stefanos Kargas
Stefanos Kargas

Reputation: 11053

Java - get "program files" path

How can I get the current computer's "Program Files" path with Java?

Upvotes: 27

Views: 26106

Answers (4)

NiksD
NiksD

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

azvampyre
azvampyre

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

Michael
Michael

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

Riduidel
Riduidel

Reputation: 22292

Simply by calling System.getenv(...)

System.getenv("ProgramFiles");

Notice it will only work in Windows environments, of course :-)

Upvotes: 40

Related Questions