Robert
Robert

Reputation: 42640

IntelliJ: Add a Java SDK using JAVA_HOME variable

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?

Upvotes: 0

Views: 2678

Answers (1)

Ambro-r
Ambro-r

Reputation: 929

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

Upvotes: 2

Related Questions