Skip to content

Instantly share code, notes, and snippets.

@Ebrahim-Mostafa
Last active May 20, 2023 22:52
Show Gist options
  • Save Ebrahim-Mostafa/d04444b867bc34244feb9e9ff5f0f380 to your computer and use it in GitHub Desktop.
Save Ebrahim-Mostafa/d04444b867bc34244feb9e9ff5f0f380 to your computer and use it in GitHub Desktop.
Switching between multiple JDK on macOS
#!/bin/zsh
#In order to execute the shell file commands:
#sh jenv.sh
#chmod +x jenv.sh
#./jenv.sh
echo " ______ _ _ _ _______ __ __ _ _ "
echo " | ____| | | | (_) |__ __| | \/ (_) | |"
echo " | |__ | |__ _ __ __ _| |__ _ _ __ ___ | | ___ _ __| \ / |_ _ __ __ _| |"
echo " | __| | _ \| __/ _ | _ \| | _ _ \ | |/ _ | __ | |\/| | | _ \ / _| | | |"
echo " | |____| |_) | | | (_| | | | | | | | | | | | | __| | | | | | | | | | (_| | |"
echo " |______|_.__/|_| \__,_|_| |_|_|_| |_| |_| |_|\___|_| |_| |_|_|_| |_|\__,_|_|"
brew install jenv
# .zprofile should be updated instead of .zshrc file in case of working on MacOS M2 Ventura
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(jenv init -)"' >> ~/.zshrc
#These 2 lines should be uncommented when using MacBook pro M2 ventura >>
#echo export 'JAVA_HOME="$HOME/.jenv/versions/`jenv version-name`"' >> ~/.zshrc
#echo 'alias jenv_set_java_home='export JAVA_HOME="$HOME/.jenv/versions/`jenv version-name`"'' >> ~/.zshrc
source ~/.zshrc
/usr/libexec/java_home -V
#jenv add /Library/Java/JavaVirtualMachines/FileName/Contents/Home
search_dir=/Library/Java/JavaVirtualMachines
for entry in "$search_dir"/*/Contents/Home
do
echo "$entry"
jenv add "$entry"
done
#Adding Your Java Environment,Add the installed java to jenv:
#EX: jenv add /Library/Java/JavaVirtualMachines/JAVA_FILE_NAME/Contents/Home
#To see all the installed java command will give the list of installed java::
##jenv versions
#Configure the java version which you want to use:
#jenv global 1.8.0.291
#--------------------------------------------------------------------------
#References:
#How do I install Java on Mac OSX allowing version switching?
#https://stackoverflow.com/questions/52524112/how-do-i-install-java-on-mac-osx-allowing-version-switching
#How to get the list of files in a directory in a shell script?
#https://stackoverflow.com/questions/2437452/how-to-get-the-list-of-files-in-a-directory-in-a-shell-script
#Jenv Github project:
#https://github.com/jenv/jenv
#https://www.jenv.be/
#Useful Links:
#change java version in macOS BigSur:
#https://stackoverflow.com/questions/64062444/change-java-version-in-macos-bigsur
#How to set or change the default Java (JDK) version on macOS?
#https://stackoverflow.com/questions/21964709/how-to-set-or-change-the-default-java-jdk-version-on-macos
#https://stackoverflow.com/questions/21964709/how-to-set-or-change-the-default-java-jdk-version-on-macos/24657630#24657630
#https://stackoverflow.com/questions/21964709/how-to-set-or-change-the-default-java-jdk-version-on-macos/44169445#44169445
#java version update to 11 still shows 8 [duplicate]
#https://stackoverflow.com/questions/70313852/java-version-update-to-11-still-shows-8
#Mac OS X and multiple Java versions:
#https://stackoverflow.com/questions/26252591/mac-os-x-and-multiple-java-versions
#Unable to create Z shell alias to "activate" Java 8 (OpenJDK) on macOS
#https://stackoverflow.com/questions/70557354/unable-to-create-z-shell-alias-to-activate-java-8-openjdk-on-macos
#How to switch between Java LTS versions 8, 11 and 17 on Mac:
#https://www.lotharschulz.info/2021/11/30/how-to-switch-between-java-lts-versions-8-11-and-17-on-mac/
#Switching Java (JDK) Versions on MacOS:
#https://medium.com/@devkosal/switching-java-jdk-versions-on-macos-80bc868e686a
#Installing & switching between multiple JDK on macOS:
#https://medium.com/@manvendrapsingh/installing-many-jdk-versions-on-macos-dfc177bc8c2b
#Github Scripts:
#https://gist.github.com/trmaphi/3bc5b5a397a9491a61787143b098da85
#https://github.com/JoniVR/JavaVersionSwitcher
#https://gist.github.com/turesheim/9486922d13324576104ee083a1775785
#https://gist.github.com/kenglxn/1843d552dff4d4233271
#Jenv not setting JAVA_HOME #44
https://github.com/jenv/jenv/issues/44
#Getting a warning when installing homebrew on MacOS Big Sur (M1 chip) [closed]
https://stackoverflow.com/questions/65487249/getting-a-warning-when-installing-homebrew-on-macos-big-sur-m1-chip
#How to install an homebrew package behind a proxy?
https://apple.stackexchange.com/questions/228865/how-to-install-an-homebrew-package-behind-a-proxy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment