5

i`m trying to get the default Program Files folder on java. When I use:

 System.getenv("ProgramFiles")

It returns "C:\Program Files" instead of "C:\Program Files (x86)"

I can add manually +(x86) but if the user will use 32bit system it will be the wrong folder.

adarshr
  • 61,315
  • 23
  • 138
  • 167
DanM
  • 1,530
  • 4
  • 23
  • 44

3 Answers3

9

You should be using

System.getenv("ProgramFiles(X86)")

You can find the full reference on Wikipedia.

adarshr
  • 61,315
  • 23
  • 138
  • 167
2

This is the correct answer for 32-bit Program Files directory

System.getenv("ProgramFiles(X86)")

However if a programmer is looking for the 64-bit Program Files folder but running a 32-bit JVM, System.getenv("ProgramFiles") will return "\Program Files (x86)\" as a side effect of 32-bit compatibility. In some cases, a programmer will still want the 64-bit ProgramFiles directory. This solution has its flaws, but will usually work...

System.getenv("ProgramFiles").replace(" (x86)", "")

Which is only marginally better then

System.getenv("SystemDrive") + "\Program Files"    

-Tres

tresf
  • 7,103
  • 6
  • 40
  • 101
-2

Possibly a try and catch.

try {
  System.getenv("ProgramFiles(X86)");
}
catch (Exception e) {
  System.getenv("ProgramFiles");
}

Maybe the Exception could be more specific but that general idea.

Nicholas
  • 13
  • 1