Core Java

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)

Paris Apostolopoulos

Paris is a senior software engineer focusing on J2EE development, loves Business process modelling and is keen on software quality challenges. He is passionate about Java and Java communities. He is a co-founder and administrator of the first Java User Group in greece(JHUG.gr) and occasional speaker on meet-ups and seminars and regular blogger. For his contributions and involvement on the Java community he has been awarded the title of Java Champion in 2007 by Sun Microsystems.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Pete
Pete
7 years ago

Installing java7 failes here. Do you have any thoughts?

$ brew untap caskroom/versions && brew tap caskroom/versions ; brew update
Untapping caskroom/versions… (198 files, 258.4KB)
Untapped 0 formulae
==> Tapping caskroom/versions
Klone nach ‘/usr/local/Homebrew/Library/Taps/caskroom/homebrew-versions’ …
remote: Counting objects: 180, done.
remote: Compressing objects: 100% (176/176), done.
remote: Total 180 (delta 12), reused 32 (delta 2), pack-reused 0
Empfange Objekte: 100% (180/180), 70.26 KiB | 0 bytes/s, Fertig.
Löse Unterschiede auf: 100% (12/12), Fertig.
Tapped 0 formulae (198 files, 258.4KB)
Already up-to-date.

$ brew cask install caskroom/versions/java7
Error: ‘/usr/local/Homebrew/Library/Taps/caskroom/homebrew-versions/Casks/java7.rb’ does not exist.
Error: nothing to install

Paris Apostolopoulos
7 years ago
Pete
Pete
7 years ago

But Cask java6 is still there, and it seems to work. Regarding java7 it might be an issue with not updating the Cask formula to use new API methods of brew: https://github.com/caskroom/homebrew-cask/issues/31968 . The Cask java7 used ‘MacOS.release’ but should use ‘MacOS.version’.

Pete
Pete
7 years ago

And the reason why java7 is pulled is that it cannot be downloaded from oracle without login. https://github.com/caskroom/homebrew-versions/pull/3914

Back to top button