0

If I open the command prompt, go to my application folder and type the following:

java -version

I'll get the following result:

java version "11.0.8" 2020-07-14 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.8+10-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.8+10-LTS, mixed mode)

But if I go to Spring Tool Suite > right click project name on Project Explorer > Show in > Terminal > and type the same command (in the exact same folder as above):

java -version

I get a different result:

openjdk version "14.0.2" 2020-07-14
OpenJDK Runtime Environment (build 14.0.2+12-46)
OpenJDK 64-Bit Server VM (build 14.0.2+12-46, mixed mode)

How can it be?

Result when typing echo %PATH% in the command line:

C:\Program Files\Java\jdk-11.0.8;C:\Program Files\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\Oberthur Technologies\AWP\Dlls;C:\Program Files\Oberthur Technologies\AWP\Dlls;C:\Program Files\Git\cmd;C:\Program Files\Java\jdk-11.0.8\bin;C:\Program Files\apache-maven-3.6.3\bin;C:\Program Files\MySQL\MySQL Shell 8.0\bin\;C:\Users\Samsung\AppData\Local\Microsoft\WindowsApps;C:\Program Files\heroku\bin

Result when typing echo %PATH% in the STS terminal (same folder as above):

C:/spring-tool-suite-4-4.8.0/contents/sts-4.8.0.RELEASE//plugins/org.eclipse.justj.openjdk.hotspot.jre.full.
win32.x86_64_14.0.2.v20200815-0932/jre/bin/server;C:/spring-tool-suite-4-4.8.0/contents/sts-4.8.0.RELEASE//p
lugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_14.0.2.v20200815-0932/jre/bin;C:\Program File
s\Java\jdk-11.0.8;C:\Program Files\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Ora
cle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerSh
ell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\Oberthur Technologies\AWP\Dlls;C:\Program File
s\Oberthur Technologies\AWP\Dlls;C:\Program Files\Git\cmd;C:\Program Files\Java\jdk-11.0.8\bin;C:\Program Fi
les\apache-maven-3.6.3\bin;C:\Program Files\MySQL\MySQL Shell 8.0\bin\;C:\Users\Samsung\AppData\Local\Micros
oft\WindowsApps;C:\Program Files\heroku\bin;C:\spring-tool-suite-4-4.8.0\contents\sts-4.8.0.RELEASE;
jkfe
  • 549
  • 7
  • 29
  • The `PATH` in the command prompt is different from the `PATH` in the STS terminal. Check the `PATH` is both for yourself to learn the difference. – Andreas Feb 19 '21 at 21:34
  • the path is the same on both. The command is being ran on the application root folder. – jkfe Feb 19 '21 at 21:41
  • 2
    Since the `java` command is found by searching the `PATH`, if the `PATH` are the same, they would find the same version of Java. Note that I'm not talking about the "path" to the current folder, I'm talking about the environment variable named `PATH`. – Andreas Feb 20 '21 at 03:08
  • I see. What you mean by "being the same"? There is more than one PATH variable? I have just one. It is called "Path"(lower case) and has several entries. Among them: C:\Program Files\Java\jdk-11.0.8; C:\Program Files\Common Files\Oracle\Java\javapath; C:\Program Files (x86)\Common Files\Oracle\Java\javapath; %JAVA_HOME%\bin. And the JAVA_HOME variable points to C:\Program Files\Java\jdk-11.0.8. – jkfe Feb 21 '21 at 11:26
  • Every process has it's own set of environment variables, so every process may potentially have a different value for the `Path` variable. STS has likely changed the `Path`, so the value for `Path` in the STS terminal will be different from the one you see in a command prompt. Well, it would be if you look at it in the STS terminal. – Andreas Feb 21 '21 at 15:57
  • If I go to STS > Project > Properties > Java Build Path > Libraries > Modulepath > It says "JRE System Library[JavaSE-11]". Where in STS could this be pointing to 14 then? – jkfe Feb 22 '21 at 12:41
  • You still haven't confirmed that the path in the STS terminal is different. Please edit the question and show us both the command prompt and the STS terminal `PATH` values. My guess is that some Java 14 directory is in the beginning of the STS terminal version, and that directory is what you're looking for in the STS setup. It could e.g. be the `PATH` that STS itself is running with, but we can't know until we see it. – Andreas Feb 22 '21 at 19:19
  • @Andreas Not sure what are the PATH variables that you need me to paste here. Windows environment variable Path has the value of "C:\Program Files\Java\jdk-11.0.8;C:\Program Files\Common Files\Oracle\Java\javapath;....". STS > Project > Properties > Java Build Path > Libraries > Modulepath > "JRE System Library[JavaSE-11]". Is this what you need? If not please let me know how I can get the value for those PATH variables that you are looking for and I'll do that. Thanks. – jkfe Feb 22 '21 at 20:42
  • To see the effective `PATH` for the process running commands, type `echo %PATH%` on the command line (Command Prompt *and* STS Terminal). – Andreas Feb 22 '21 at 21:23
  • @Andreas I updated the question adding the result for ```echo %PATH%``` – jkfe Feb 22 '21 at 21:41
  • As you can see, the very first entry is the directory of a JRE 14.0.2 that ships with STS: *C:/spring-tool-suite-4-4.8.0/contents/sts-4.8.0.RELEASE//plugins/org.eclipse.justj.openjdk.hotspot.jre.full. win32.x86_64_**14.0.2**.v20200815-0932/jre/bin/server* – Andreas Feb 22 '21 at 22:00
  • Yes, I see. What are the implications and purpose of these PATH variables being different? I want to use java 11, not java 14. So shouldn't it all be set to 11? – jkfe Feb 22 '21 at 22:11
  • Btw, if you want to add this as a response to the question, I'll check the answer as accepted. – jkfe Feb 22 '21 at 22:15

1 Answers1

3

Disclaimer: The following answer is for Windows, and testing was done using Eclipse IDE 2020-12 for Enterprise Java Developers.

After some testing, I have found the following quirk regarding the PATH of the Terminal.

In the eclipse.ini file, you (can) specify which Java installation to use to run Eclipse itself, using the -vm option. See: How to specify jdk path in eclipse.ini on windows 8 when path contains space

On older versions of Eclipse, the value was unspecified, which meant that Eclipse would use the default Java installation, i.e. the Java installation found on the PATH. In newer version of Eclipse, the value defaults to a JRE that ships as a plugin with Eclipse.

If the -vm value is a directory, that directory is prepended to the PATH when opening a Terminal using Ctrl+Alt+T in Eclipse, which means that the version of Java seen by the Terminal will be different from the version seen in a Command Prompt opening outside of Eclipse.

If the -vm value is the fully qualified name to javaw.exe, the PATH is unaffected, i.e. it will be the same as the PATH seen by a Command Prompt.

To check the PATH used by Terminal, run command echo %PATH%. Check the first entry to see if it refers to a particular Java installation.

If you want to ensure that Terminal has the same PATH as you'd see in a Command Prompt, change the value of the -vm option to fully qualify javaw.exe.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Cool. I opened SpringToolSuite4.ini and replaced ```-vm plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_14.0.2.v20200815-0932/jre/bin``` by ```C:\Program Files\Java\jdk-11.0.8\bin``` and it worked! Now when I go to the terminal and enter ```java -version``` I'm getting ```java version "11.0.8" 2020-07-14 LTS```. Answer accepted. Thanks a lot. – jkfe Feb 22 '21 at 23:39