Search code examples
javawindowspath-variables

Paths to multiple JDK's in windows system variable PATH


Imagine, in my Path variable on Windows 7 I have the path to my jdk version 8 already included. Now I start a new project where I am using Java 11 for example and I add the path to java 11 JDK's bin also to the Path variable. I am wondering, if it is required to remove the path to the (old) JDK 8 in this case from the Path variable for a correct working? If I leave it there, will the correct Java version be picked up for the new project? And also: In my IDE, when creating a new project, I also do select a JDK to use. What is this choice used for and what is the specification of the JDK in the path variable used for? I am not very familiar with operating systems, so please explain it :)


Solution

  • 1) the system's path variable

    The path variable defines where the system will search for executables you are using on the console/shell.

    Having multiple JDKs in your operating system's path variable is a bad idea. One will take precedence and you cannot even (or should not) be sure which one it is.

    If you need different JDKs for different projects, you may create a script setting the environment. Let's call it configure.bat for Windows. When opening a shell you would first run that script to set all environment variables and probably start needed services.

    Use commands like java -version or mvn version to check the JDK you shell is using!

    You may alternatively be able to create the environment settings by configuring an instance of your shell in a different way, but I unfortunately can't give you any details about that.

    There is an alternative ...

    2) the IDE's path variable

    In you IDE (Eclipse, IntelliJ) you can also configure the JDK, and more important: you can configure different JDKs for specific code levels and can set the code level (or the JDK directly) on each of your projects individually. While working in the IDE you don't need to run configuration scripts, the IDE will take care of that. Be careful though, when using a system console in the IDE, you might be falling back to 1) then.

    3) Maven

    In a similar way as in the IDE you can configure:

    • JDKs (.m2/settings.xml)
    • code levels (the project's pom.xml or one of the parent POMs)

    when using Maven. For more details see: http://maven.apache.org/plugins/maven-compiler-plugin/examples/compile-using-different-jdk.html

    This should be possible for other build management tools like Gradle. I have to refer you to a web search for those, though.