45

I just started to use jenv, I followed a blog post that was explaining how to use jenv and setup multiple java version on MacOSX. But the problem now I am running into is setting up the JAVA_HOME. As I switch java environment using jenv I want to make sure JAVA_HOME on my bash_profile also changes accordingly.

How do i do that?

I have following on my ~/.bash_profile

if which jenv > /dev/null; then eval "$(jenv init -)"; fi
add-semi-colons
  • 18,094
  • 55
  • 145
  • 232

3 Answers3

108

Try the "export" plugin:

jenv enable-plugin export

You can check the Export plugin section in Readme.md at the jEnv Github repo (https://github.com/gcuisinier/jenv)

kimbaudi
  • 13,655
  • 9
  • 62
  • 74
12

For me, enabling the export plugin like kimbaudi didn't work. Adding the following code to .bash_profile (or .bashrc, .zprofile or .zshrc depending on what shell you use) did the job for me:

if which jenv > /dev/null; then eval "$(jenv init -)"; fi

This was in the troubleshooting page, but they seemed to state it was in the instructions guide, which it wasn't.

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
6

You can add these alias in your ~/.bash_profile file and easily switch between different JAVA versions.

export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)

alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME’
  • To check the available java versions in your system use

    jenv versions
    
    jenv version
    
  • You can even set particular version for single project by using following command in private terminal.

    jenv local 11.0
    

# Seconds option :-

Download adopt jdk from your choise from file -> project structure -> project sdk and then add following function in your .zshrc to switch between different version as per requirement.

[Attaching screenshot for reference:] :-

enter image description here

Switch between different JDK versions

Add the below function in your ~/.bashrc or ~/.zshrc

jdk() {
        version=$1
        export JAVA_HOME=$(/usr/libexec/java_home -v"$version");
        java -version
 }
  • Source the profile and you can change the version like below:

jdk 1.8 jdk 9 jdk 11 jdk 13

Vrushali Raut
  • 1,130
  • 1
  • 10
  • 20