2

I have a simple java project and I try to change java version in Intellij from java 11 to java 8. It isn't a maven project. I found something on google and it looks easy, but it's not working.

So, I pressed ctrl + shift + alt + s and here I changed jdk11 with jdk1.8:

enter image description here

But now if I run java -version on Intellij terminal I get this in the console:

java version "11.0.14" 2022-01-18 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.14+8-LTS-263)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.14+8-LTS-263, mixed mode)

It looks that it continue to use java 11. What else should I do? Thank you!

elvis
  • 956
  • 9
  • 33
  • 56
  • 1
    The terminal is just that: a terminal. It will use whatever `java` executable it can find on the `$PATH` / `%PATH%` first. In your case, this seems to be a Java 11 JRE. – Turing85 Nov 12 '22 at 12:47
  • 1
    Try to print result of `System.getProperty("java.version")` like `System.out.println(System.getProperty("java.version"));` to see what java version is used by IDE to *run* your code. – Pshemo Nov 12 '22 at 12:49

1 Answers1

4

Changing the JDK in IntelliJ UI does not affect its Intellij terminal:

When changing the JDK, we need to remember that this only affects the JDK used by IntelliJ. Therefore, when running the Java project via the command line, it'll still use the JDK specified in the JAVA_HOME environment variable

So when running java on Intellij terminal, you still have to set JAVA_HOME to change java version

Thaiminhpv
  • 314
  • 2
  • 10
  • 2
    This is only partial correct. It depends on the exact local setup. The `$PATH` / `%PATH%` variable is the determining factor on what JDK is used (although it is best practice to put `$JAVA_HOME/bin` / `%JAVA_HOME%/bin` on the `$PATH` / `%PATH%`). – Turing85 Nov 12 '22 at 12:53