Java Versioning and Installation
Install and switch in between Java versions, quickly and conveniently.
Java is a class-based, object-oriented programming language.
Today It’s so easy to use java with the help of ide (eg. IntelliJ IDEA, eclipse). But sometimes it is so hectic to switch in between java versions, as we need to compile our project in java 8, java 11, java 6 etc. and it becomes time taking task to do that.
Install Java, straight forward method
Look for version
>> brew search java
>> brew tap adoptopenjdk/openjdk>> brew install --cask adoptopenjdk8
>> brew install --cask adoptopenjdk11
Install in Linux
>> apt install default-jre
>> apt install openjdk-8-jre-headless
>> apt install openjdk-11-jre-headless
Then we need to set up PATH in environments
Checking for version and path
>> java -version
>> /usr/libexec/java_home -V
with something better
Java Environment (jenv)
jenv is an environment/version manager for java, that it’s so convenient to switch in between the java versions.
How to install?
>> brew install jenv
the brew is an open-source package manager for macOS for Linux just replace it with apt or apt-get
Set PATH environment
If you are using bash then you need to add the PATH in ~/.bash_profile or ~/.bashrc, in the case of zsh you need to add it inside ~/.zshrc.
What are these files? these files basically act as a prescript for any terminal (bash, zsh etc.), that executes before each time when you open your terminal.
Append below the line in those files
eval “$(jenv init -)”
Note: make sure that jenv is executable in your bash else you need to install it properly or set PATH.
We’re all set, Let’s do some experimenting
Check for the java version
>> java -version
Add local java to jenv
>> jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home
you’ll get a path using the java_home command as used at the start
Show all local available versions
>> jenv versions
Set local/global java for terminal
>> jenv local 1.8
>> jenv global 1.8
For more commands for jenv
>> jenv commands
Thanks for reading :)