I am using java-8 for multiple spring-boot projects but now one of them needs to be shifted to java-11. So I need to install both versions on my laptop. So how to handle different JAVA versions for different projects on the same machine?
3 Answers 3
Use SDKMAN https://sdkman.io/. That can help you to solve this issue.
You can give commands like:
sdk use java 8.282.08.1-amzn
or
sdk use java 11.0.10.hs-adpt
Comments
Each instance of JDK/JRE is installed into its own directory.
E.g. I have multiple JDK version in my /opt/java
directory.
ls -1 /opt/java/
jdk-11.0.1
jdk1.8.0_171
openjdk-11.0.2
openjdk-14.0.1
I can switch between the versions by specifying full path to the java executable file.
E.g. running on java 11:
/opt/java/jdk-11.0.3/bin/java -jar my-springboot-app.jar
running on java 14:
/opt/java/openjdk-14.0.1/bin/java -jar my-springboot-app.jar
If you are using Unix-like system, you can also look at alternatives util. It helps to switch between different versions of applications (different java versions).
Comments
Each IDE is a separate let's say "ecosystem", and within that IDE you can set your ( in eclipse example ) java compiler and java build path java version. Yes, you will need to install both of them on your machine, but there will be no collision between them if you set your working environment correctly within your IDEs. The same would go for an example docker images.
If you fancy a terminal you can create a command that will switch for example MAVEN_HOME or JAVA_HOME to a specific JDK version
How to set JAVA_HOME in Linux for all users
Switching between different JDK versions in Windows
Each terminal has separate session so when you run your application on JDK8, open a new terminal, switch version, your other application will run without any issues on JDK11.
-source ...
) and target- (-target ...
) version. See this question for details. Thus, we can just install java 11 and use it for all projects using a version<=
11. If we want to install multiple JDK versions, the actual procedure depends on the OS we are using..class
files. Since java is downwards-compatible, we can use, for example a java 11 jre to run a java 8 project.