Sunday, April 23, 2017

Setting multiple Java JRE/ JDK on MacOSX using brew, cask and jenv

Yesterday at the Java9, Jigsaw HackTheTower event, I realized that I need to step up my game and improve my existing mechanism on maintaining several different JDK's on my machines.

I used to manually download the jdk's, or install them using brew cask, and I would set 'bash alias' on my `~/bash_profile` to switch between different 'JAVA_HOME' etc etc.

I am already using brew & brew cask (official site here) & i recently started using 'CakeBrew'. So in order to install 3 different versions of java all you need to do is :

Step 1: Install JDK's using brew and  brew cask

brew cask install caskroom/versions/java6
brew cask install caskroom/versions/java7  
brew install java

After the installation check the following folder, you are expected to see the 3 different JDK folders.

cd /Library/Java/JavaVirtualMachines

Step 2: Install jenv

> brew install jenv 

Step 3: Add the 3 available JDK's to jenv

jenv add /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home

Step 4: Check if jenv has registered the different jdk's

jenv versions

Step 5: Use jenv to set up the JDK env either globally or the current shell

--Setting java 1.8 for the shell
jenv shell 1.8
java -version 
jenv shell 1.7
java -version 
-- this sets it globally
jenv global 1.8
java -version


All done! You can switch different versions easily!

Watch out for now

  • You can install a pre-release of java9 using ' brew cask install caskroom/versions/java9-beta' but it seems that the way it is installed and the paths are not compatible with what jenv expects so you can not jenv add 1.9 (for the time being)

No comments:

Post a Comment