I noticed starting with version 8 that the Java installer now prepends to the Windows PATH variable C:\ProgramData\Oracle\Java\javapath.

For most casual users and developers that will only have Java 8 installed and not other versions then this should be fine. You can readily use the commands java or javac from the command prompt.

If you’re like me, you have different tools that have specific Java version requirements so you install multiple versions of Java. For example, versions 7 and 8, both the 32-bit and 64-bit editions. However, to support multiple versions you must update your Windows PATH.

Set JAVA_HOME as first entry to Path Variable

Since the installer prepended the Oracle Java path to the front of the Windows PATH variable, I needed to create my own custom JAVA_HOME environment variable pointing to the directory of Java 7 then prepend %JAVA_HOME%\bin to my Windows PATH variable so that it was the first search directory. If I suffixed it to the end of the Windows PATH variable then Java 8 was always being picked up (Windows searches the directories left-to-right in the PATH variable).

java_home_screenshot java_home_prepended_path_screenshot

Why not just remove the Oracle Java path entry and not worry about your JAVA_HOME placement in the Path?

You might suggest to simply remove the Oracle Java path that was prepended to your Windows PATH by the installer. Yes, I thought of that too, but the installer also copies the java.exe, javaw.exe, and javaws.exe files to C:\Windows\System32 directory, which is also in the Windows PATH variable. So rather than chase down all the possible directories that the installer put Java, and to avoid deleting the System32 files that actually may be necessary to some process somewhere, I decided that simply prepending my preferred Java Home directory to the path was best option.

java_system32_screenshot

Conclusion

So there you have it, as with prior versions of Java, if you have multiple installed you still have to setup and add JAVA_HOME to your Windows PATH variable, but starting with Java 8 you now must be particular about adding the JAVA_HOME entry at the front of the path.


Resources