The Java Development Kit (JDK) is a cross-platformed software development environment that offers a collection of tools and libraries necessary for developing Java-based software applications and applets. It is a core package used in Java, along with the JVM (Java Virtual Machine) and the JRE (Java Runtime Environment). To install JDKs, Follow this link. During installation remember your destination folder that is your drive and directory where you are saving your JDK and the requirement of this will be known later.
Step by Step Implementation
Look at the picture below, we have installed two JDK’s of version 16 and 17 respectively in the folder F:\Java. According to your requirement, You can download JDKs of different versions and save them into different folders on different drives.
Now open Command Prompt then type java and press enter. You will definitely see an error message like in the below picture. The reason for getting this error is, the command java is an executable inside the bin directory of our downloaded JDK, hold on! don’t read further, please look into the bin directory of your downloaded JDK, you will definitely find this executable having extension .exe. But the question is, why my command prompt is not able to locate that executable?
Whenever we type any executable in the command prompt as a command, our windows operating system will try to find the location of that executable, if it fails to find we’ll get an error. In windows, locations of all the executables are stored inside an environment variable is known as path. As we haven’t specified the location of our java executable in the path environment variable that is F:\Java\jdk-16.0.2\bin and this path will vary from JDK version to version, that’s why we got the above-shown error. To check the value of path environment variable in windows, click on the type to search symbol which is next to the start symbol, then type Environment, one option will be there having name Edit The System Environment Variables, click on that
After clicking on the option in the above picture, one GUI application will be opened. Then click on the Environment Variables Button of that particular GUI application screen.
After clicking Environment Variables Button, we got something like this in the below picture. Jump to section System Variable, search variable path and select it, then click on the Edit button.
After clicking on the Edit Button, the below picture is the result. As you can see, we don’t have the path F:\Java\jdk-16.0.2\bin, provided to the path environment variable.
Now click on New Button in the above picture and add the value F:\Java\jdk-16.0.2\bin. Then press ok and continue pressing ok till that GUI application is closed automatically. Now again open the Edit The System Environment Variable, then check the updated value for the path environment variable. See the below picture for confirmation.
Now open the command prompt, if it’s opened before close it first, and open it again. Then type java You must have seen output like in the below picture.
Now come to the main part of the question, how to use multiple JDKs, we have set a path to bin folder for one of my JDK that is JDK version 16 in my path environment variable, we can use that JDK to compile and run my java program. Suppose we want to use JDK version 17, then I have to add the path to the bin folder of JDK version 17 to the path environment variable and that path is F:\Java\jdk-17.0.2\bin. Follow the above-described steps to add values to the path environment variable and remember these paths are specific to our example. See the below picture for confirmation.
Now you can see, we have paths to all executables inside the bin directories of your respective JDKs, in our case that is of versions 16 and 17. One important thing to remember here is, whenever you have multiple paths to the bin directory of different JDKs in your path environment variable, the first path value is always selected and the rest values are ignored. Now open the command prompt, if it is opened before, close it first and open it again. Type command java -version, see what it prints, in our case, it is printing java version “16.0.2”, as the path to the bin directory of JDK version 16 tops the list of paths to the bin of JDK. Look at the above picture for confirmation.
Whenever you have to use JDK according to your choice, make sure the path to the bin folder of that particular JDK should be at the top among all the paths to the bin of respective JDKs. Whenever we see the values of the path environment variable, you will see buttons on the right side of that GUI application window having labels MOVE UP and MOVE DOWN, you can use these buttons to place your JDK’s bin path values accordingly. Suppose, we want to use JDK of version 17, we have to place the path to the bin of that particular JDK on top. First, select the path F:\Java\jdk-17.0.2\bin.
Then click on the Move up button accordingly and press ok, continue pressing ok till that GUI application is closed automatically. Now again check the updated values for the path environment variable. Look at the below picture for confirmation.
Now open the command prompt, if it’s opened before, close it first and open it again. Then type the java -version command and see the result. You can see it’s printing java version “17.0.2”, as the path to the bin folder of JDK-17 tops the list of all the paths to the bin of different JDKs, in our case that is between JDK-16 and JDK-17. Now you can use JDK-17 to compile and run your java programs.
This is one of the ways you can download multiple JDKs and use them according to your choice.