0

On Windows the path of the JDK typically includes the version string. Therefore if you upgrade the JDK the path name changes. In IntelliJ this allyways means to manually update all locations where the JDK path is saved.

Therefore I want to add a SDK with an Java home path that use the JAVA_HOME variable as this is automatically set when installing an JDK and therefore would not require any updates later.

IntelliJ Project Structure Dialog für SDK

The main problem is that the Java home path text boy is not directly editable. One can only open a file/directory picker dialog that has an edit field but does not allow to use variables like $JAVA_HOME$ or %JAVA_HOME% (shows only an error message "Specified path can not be found").

How can I add an Java SDK using the JAVA_HOME environment variable that automatically updates whenever I upgrade the installed JDK?

Robert
  • 39,162
  • 17
  • 99
  • 152

1 Answers1

2

You could always create a symbolic, or “soft" link pointing to whatever JDK you are using. Then you can set-up IntelliJ to use the symbolic link location and just update the symbolic link to point to whatever version of Java you are using.

So, for example in C:\Program Files\Java\ you could run the following:

mklink /D myJDK "C:\Program Files\Java\jdk1.8.0_112"

Then in IntelliJ you just add C:\Program Files\Java\myJDK as your SDK as this symbolic link is actually pointing to C:\Program Files\Java\jdk1.8.0_112

Windows does not support changing of the link (though there are some tools online which do), but the easiest would just be to delete the link and re-create it.

You could then also set-up your systems JAVA_HOME to point to this link as well.

Oh, when creating the symbolic link, you'll need to have administrative access.

The Complete Guide to Creating Symbolic Links (aka Symlinks) on Windows

Ambro-r
  • 919
  • 1
  • 4
  • 14
  • But in the end you have to manually maintain this soft-link. Therefore it is just a question if you manually update the softlink outside of IntelliJ or main the SDK-location inside of IntelliJ. – Robert Nov 11 '19 at 08:29
  • Yes, you will need to manually maintain it, but the same could be said for setting `JAVA_HOME` and I don't think that IntelliJ will recognize it. I guess that if you want everything to be "automatic" you could always try write a clever cron script (i.e. maybe a powershell script) that checks your java directory and updates the soft-link accordingly. – Ambro-r Nov 11 '19 at 09:44
  • This is only the case for the Oracle JDK releases. The AdoptOpenJDK releases do allow to automatically update the JAVA_HOME variable by the installer. Because of the Oracle license problem I switched completely to AdoptOpenJDK. – Robert Nov 11 '19 at 10:38