As JDK versions keep coming one in every year, it is difficult to keep track of what runs in which version and you might want to switch between version to execute applications, to learn the newer features of the current version and so on. It is possible to install different versions of JDK in the same box. But every time you go to your command prompt, whatever java path settings you might have set in your environment variable will what be reflected. And if you want to switch to another version you have to change the value of the PATH environment variable to point to the version you want.
Here is a neat way to have a shortcut to get into the command prompt for every jdk version you have in your system.
- Open Notepad
- Key in the following
- Save the file as say tiger.bat under the folder say c:\
- Now right click on your desktop and select New->Shortcut
- In the program location, enter the following
cmd /k c:\tiger.bat
- Click Next and give the shortcut name, say Tiger (JDK1.5)
- Click Finish
- Now double click on the shortcut and in the command prompt type the following command
Got it? You can create a batch file and a shortcut for every JDK you want just by altering the path of the JDK location in the batch file above. So lets go through whats happening here. DOS provides a way of executing commands in batch which means it can parse through a text file and can execute if that line contains a command it understands. Usually this file is saved with an extension “bat”. So in the above batch file, tiger.bat we use the set command to set the environment variables for that session. The first line @echo off says that the cmd program should suppress displaying the command it is executing. Ofcourse it doesnt stop cmd from displaying any command errors or user messages that can be displayed using the echo command.
OK once we have this file ready, we are creating a shortcut to cmd with an argument of this batch file to be executed when the shell is opened. That’s fine but what is the /k doing there? Well whenever you call cmd passing an argument it interprets and takes appropriate action and then quits. The flag /k tells cmd that it should not quit and wait for more inputs at the prompt. Try removing the /k and see the result for yourself.