2

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

4
  • 1
    Using sdkman to manage your jdk version Commented Jul 17, 2022 at 5:47
  • 2
    JAVA_HOME is not PATH-like, you can't put more than one JVM on the list. Essentially you change it on the fly - tools like sdkman do this for you. Commented Jul 17, 2022 at 5:53
  • I see, since I already installed a few Java versions, before I install sdkman, should I uninstall those before reinstalling it using sdkman? incase those Java versions etc are not in any default locations for sdkman @rzwitserloot Commented Jul 17, 2022 at 5:56
  • One more tool 😊: I recommend jEnv to handle different java versions on a system. Commented Jul 17, 2022 at 10:15

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.