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.
On a 64-bit version of Windows, 64-bit programs are stored in the “C:\Program Files” folder and 32-bit programs are stored in the “C:\Program Files (x86)” folder.
In the menu, click Settings > Active Profile. Click the Java icon and then the Advanced tab. Select 32-bit Java (default) or 64-bit Java.
Android Studio and Android do not use a java from Oracle or Sun but use OpenJDK that is an open sourced java instead, so there won't be a java folder under Program Files if you didn't installed java for other uses. Android Studio's java can be found in C:\Program Files\Android\Android Studio\jre.
You should be using
System.getenv("ProgramFiles(X86)")
You can find the full reference on Wikipedia.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With