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?
-
1The java compiler supports parameters to control the source- (`-source ...`) and target- (`-target ...`) version. See [this question](https://stackoverflow.com/questions/15492948/javac-source-and-target-options) 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. – Turing85 Dec 05 '21 at 18:42
-
how will I run my spring-boot then with diff version? – SHIVAM JINDAL Dec 05 '21 at 18:45
-
1I do not fully understand the question. The target version is "baked-in" the `.class` files. Since java is downwards-compatible, we can use, for example a java 11 jre to run a java 8 project. – Turing85 Dec 05 '21 at 18:47
-
Running multiple versions of java on the same box is something that many people dealt with in the past. It feels like you didn't do any research prior posting your question. – GhostCat Dec 05 '21 at 20:25
3 Answers
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

- 1,513
- 12
- 26
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.

- 1,907
- 2
- 10
- 20
-
is there any IDE independent solution? I generally prefer Terminal base solution to have proper understanding. – SHIVAM JINDAL Dec 05 '21 at 18:43
-
Build Tools such as gradle as well as Maven have properties for the source and target compatibility this ensures, that you are not compiling a Java 11 project with Java 8. Furthermore you can also set the Java Home variable for each terminal you are using to a different Java version. That should be pretty much it – triplem Dec 05 '21 at 19:00
-
In that case, 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 https://stackoverflow.com/questions/24641536/how-to-set-java-home-in-linux-for-all-users https://stackoverflow.com/questions/26993101/switching-between-different-jdk-versions-in-windows – zawarudo Dec 05 '21 at 20:06
-
@SHIVAMJINDAL So you prefer to work with terminals, but you have never before heard about using setup scripts to use ENV variables, to say, switch between JDKs? – GhostCat Dec 05 '21 at 20:23
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).

- 1,434
- 9
- 24