Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change java version in mac os

Tags:

java

macos

My Java version is 15.0.1(default), I want to change it to java 8(1.8.0_281).

I downloaded java8, and tried several things but the version doesn't change..

  1. change name of /Library/Java/JavaVirtualMachines/jdk-15.0.1.jdk/Contents/Info.plist to Info.plist.disabled

  2. change version in ~/.bash_profile and run source ~/.bash_profile

  3. write export JAVA_HOME=$(/usr/libexec/java_home -v 1.8) in terminal

I restarted terminal, and run java -version.. but there is still openjdk version "15.0.1"

when I write like this, /usr/libexec/java_home -V there is no 15.0.1. It return 1.8.0_281 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home

In ~/.zshrc, there is no version infomation. just export PATH="/usr/local/opt/openjdk/bin:$PATH" export PATH="/usr/local/opt/openjdk/bin:$PATH"

echo $JAVA_HOME show me /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

How to change java version?? help me plz (macOs is big sur, 11.2)

like image 553
jack0 Avatar asked Nov 01 '25 18:11

jack0


1 Answers

In mac it's pretty simple you can have two or many java versions and based on your requirement you can change that.

I found a very useful tool called jenv.

If you have already had homebrew (a package installer in mac), you can skip this step.

  1. For installing or updating homebrew

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
    
  2. Installing JEnv - java version manager a cool tool helps you to switch between multiple java env on your mac

    brew install jenv
    
  3. Adding jEnv to your path depends on the shell you are using ~/.bash_profile or ~/.bashrc , for zsh it's ~/.zshrc. Add these two lines in your bash profile file

    export PATH="$HOME/.jenv/bin:$PATH"
    eval "$(jenv init -)"
    
  4. Restart the terminal or use source ~/.zshrc

  5. Check if your JEnv is correctly installed

    jenv doctor
    

    you will get the output something like [OK] Jenv is correctly loaded

  6. for installing java 8

      brew install --cask AdoptOpenJDK/openjdk/adoptopenjdk8
    
  7. for installing java 11

    brew install --cask AdoptOpenJDK/openjdk/adoptopenjdk11
    
  8. for list down all the java on your machine

    /usr/libexec/java_home -V
    
  9. Now add these paths to jENV

    Syntax: 
    jenv add <your_jdk_path>
    
    Example:
    jenv add /Library/Java/JavaVirtualMachines/openjdk-14.0.1.jdk/Contents/Home
    
  10. All done. Check your version

    jenv versions
    
  11. For setting system wide java version

     jenv global 14.0
    
  12. For setting locally to terminal

     jenv local 11
    
  13. if you are using maven or gradle for your project

      # ensure that JAVA_HOME is correct
      jenv enable-plugin 
    
      export# make Maven aware of the Java version in use 
      (and switch when your project does)
      jenv enable-plugin maven
    

A reference guide https://www.jenv.be/

like image 55
ankursingh1000 Avatar answered Nov 03 '25 07:11

ankursingh1000



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!