In this post I will reveal how you can easily setup multiple java environments and switch between them easily.
This is a simple method I use, sharing in case anyone is interested. Please follow the steps below.
1. Download and extract the java version you require in your PC. I will be using Java 1.7 and Java 1.8 for the demonstration purpose. I have extracted java 1.7 and 1.8 to following locations.
e.g : /home/yasassri/soft/java/jdk1.8.0_05
/home/yasassri/soft/java/jdk1.7.0_65
e.g : /home/yasassri/soft/java/jdk1.8.0_05
/home/yasassri/soft/java/jdk1.7.0_65
2. Now Open the bachrc file using your favorite text editor. You can use the following commands to do this.
vim ~/.bashrc
3. Now Add the following two alias at the end of the file.
# Setting Java 8 alias setjava18='export JAVA_HOME='/home/yasassri/soft/java/jdk1.8.0_05' && export PATH=$JAVA_HOME/bin:$PATH && source ~/.bashrc' #Setting Java 7 alias setjava17='export JAVA_HOME='/home/yasassri/soft/java/jdk1.7.0_65' && export PATH=$JAVA_HOME/bin:$PATH && source ~/.bashrc'
Note : You need to replace '/home/yasassri/soft/java/jdk1.8.0_05' and '/home/yasassri/soft/java/jdk1.7.0_65' with the file location of extracted Java folders in your PC.
4. Save and exit the file.
5. Execute the following command.
source ~/.bashrc
6. Everything is set now. Now execute the following command in your terminal.
setjava18
Now check the Java version that is set with java -version, Java 8 will be set.
7. Now execute following
setjava17
Now Java 1.7 will be set.
Thanks for reading and please drop a comment if you have any issues.
Won't the $PATH variable keep corrupting if we switch version too many times? Because the var would have repeated prepending of Java 7 bin and java 8 bin for example.
ReplyDelete