Search code examples
javajava-home

setting JAVA_HOME with multiple Java versions


Previously when I was writing Java code, I only needed to add the javapath +"/bin" inside the path for environment variables and I had no major problems. Recently I have been self learning Spring and it insists on having JAVA_HOME environment variable.

However, I now have multiple Java versions on my pc. If I setup to that specific java version provided by spring initializer, the Spring application can start just fine. What is the recommended approach? Added the remaining java versions to the JAVA_HOME variable - ie. path_jdk_17;path_jdk_16.01;path_jdk_8; etc.* or do I change it on the fly whenever I use a different application or different Spring project? (for some reason at different points in time the spring initializer suggests different default java versions to use, I m recommended to stick with the default suggested)

*note: not sure if this is allowed, since when adding new javapath to path variable they explicitly have the button to add new items, but not really for JAVA_HOME


Solution

  • As in the comments section, JAVA_HOME is a variable and can not have multiple values.

    I agree with @Dolphin. To install SDKMan please read official documentation.

    Now to install a version of Java with SDKMan.

    First, list the candidates for Java available:

    sdk list java
    
    =================================================
    Available Java Versions for Linux 64bit
    =================================================
    Vendor   | Version      | Dist | Identifier
    -------------------------------------------------
    Gluon    | 22.0.0.3.r17 | gln  | 22.0.0.3.r17-gln
             | 22.0.0.3.r11 | gln  | 22.0.0.3.r11-gln
    GraalVM  | 22.0.0.2.r17 | grl  | 22.0.0.2.r17-grl
             | 21.3.1.r17   | grl  | 21.3.1.r17-grl
             | 20.3.5.r11   | grl  | 20.3.5.r11-grl
             | 19.3.6.r11   | grl  | 19.3.6.r11-grl
    Java.net | 19.ea.10     | open | 19.ea.10-open
             | 18           | open | 18-open
             | 17.0.2       | open | 17.0.2-open
             | 11.0.12      | open | 11.0.12-open
             | 8.0.302      | open | 8.0.302-open
    [...]
    

    You can install a specific version of Java using the value in the Identifier column:

    sdk install java 18-open
    

    Alternately, you can just install the default latest version:

    sdk install java
    

    Set the version of Java for a terminal session with the use subcommand:

    sdk use java 18-open
    

    To set a version as default, use the default subcommand:

    sdk default java 18-open
    

    Read more about SDKMan's usage here.