Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to best set the path to java in windows

Tags:

java

path

windows

I'm curious about the difference that exists between the paths that I see in the windows environment to java. If I do a command where java I can see

C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe
C:\ProgramData\Oracle\Java\javapath\java.exe
C:\Program Files\Java\jdk1.6.0_21\bin\java.exe

And I have seen that the first one, the javapath, gets updated with the latest java sdk I have installed. However, most of what you google tells you should set the environment variable PATH to the latest, the one with the jdk in the path. So what is the best, how do they work. For example, what's best for Xamarin or Android development?

Hopefully someone has some insight, I'd appreciate comments on this. Now I know, (everyone does) that to change the path to java you have to set the JAVA_HOME variable. I did this after posting this, it's hard to recall everything you read at any given moment! Still what's the purpose of the other paths, and why does updating java jre automatically sets the java version while downloading an sdk requires a manual update?

like image 567
gerardo flores Avatar asked Apr 27 '18 04:04

gerardo flores


People also ask

Is it necessary to set JAVA_HOME PATH?

JAVA_HOME and JRE_HOME are not used by Java itself. Some third-party programs (for example Apache Tomcat) expect one of these environment variables to be set to the installation directory of the JDK or JRE. If you are not using software that requires them, you do not need to set JAVA_HOME and JRE_HOME.

How do I find Java PATH in Windows?

Open a Command Prompt window (Win⊞ + R, type cmd, hit Enter). Enter the command echo %JAVA_HOME% . This should output the path to your Java installation folder.


1 Answers

When a new version of JRE is installed, this path C:\Program Files (x86)\Common Files\Oracle\Java appears to be updated with executables corresponding to the new JRE version. The problem is this would be JRE and not JDK. So, programs that depend on JDK (IDE for example) will fail to launch.

Here is how I get it to work consistently

  1. I always have an environment variable in User Variables (System Environment Variables by going to Control Panel -> System -> Advanced System Settings -> Environment Variables -> User Variables) -- JAVA_HOME pointing to the JDK Home. And add %JAVA_HOME%\bin to the PATH environment variable.
  2. Interestingly, I remove the entry C:\Program Files (x86)\Common Files\Oracle\Java\javapath from the Path environment variable in System Environment Variables (Control Panel -> System -> Advanced System Settings -> Environment Variables -> System Variables).

This works as of Aug 2020 for Windows 10 (1903), Java 8u251. :)

like image 173
Rakesh N Avatar answered Sep 22 '22 14:09

Rakesh N