Post

How to Set Up jEnv on macOS

Set up jEnv on macOS to quickly switch between Java versions per project.

How to Set Up jEnv on macOS

When developing Android or Java applications, managing multiple Java versions can be crucial, especially if different projects require different versions. jEnv simplifies this process by allowing you to switch between Java versions effortlessly and set JAVA_HOME per project or globally.

Here’s a quick guide to get jEnv set up on macOS:

1. Installing jEnv

The easiest way to install jEnv on macOS is using Homebrew. Open your terminal and run:

1
brew install jenv

Alternatively, if you use MacPorts, install it with:

1
sudo port install jenv

2. Configuring Your Shell

Once installed, you need to configure your shell to recognize jEnv. Depending on your shell, run the following commands:

For Bash:

1
2
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(jenv init -)"' >> ~/.bash_profile

For Zsh:

1
2
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(jenv init -)"' >> ~/.zshrc

Restart your terminal or use:

1
exec $SHELL -l

Avoid Conflicting Path Configuration

It’s important to let jEnv manage JAVA_HOME to avoid conflicting paths. You do not need to manually set JAVA_HOME in your shell configuration, as jenv will manage it automatically once the export plugin is enabled.

3. Enabling the Export Plugin

To allow jEnv to set JAVA_HOME automatically, enable the export plugin:

1
jenv enable-plugin export

Before you can add Java versions to jEnv, you need to ensure that the Java Development Kit (JDK) is properly linked to a location that jEnv can access.

Step 1: Installing Java Versions

First, install the desired Java versions using Homebrew. For example, to install Java 11 and 17:

1
2
3
4
5
# Install Java 11
brew install openjdk@11

# Install Java 17
brew install openjdk@17

After installing the Java versions, you must create symbolic links in /Library/Java/JavaVirtualMachines/, which is the standard location for JDK installations on macOS. This allows jenv and other tools to recognize the installed JDKs.

1
2
3
4
5
# Link Java 11
sudo ln -sfn $(brew --prefix openjdk@11)/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

# Link Java 17
sudo ln -sfn $(brew --prefix openjdk@17)/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk

Step 3: Adding the JDK to jEnv

Now that the symbolic links are set up, you can add the Java versions to jEnv:

1
2
3
4
5
# Add Java 11 to jenv
jenv add "$(/usr/libexec/java_home -v 11)"

# Add Java 17 to jenv
jenv add "$(/usr/libexec/java_home -v 17)"

This process ensures that jEnv can locate and manage the installed Java versions. Without the symbolic link, jEnv will not be able to find the JDK when you try to add it.

5. Managing Java Versions

You can set the Java version at three levels: global, local, or shell.

  • To set a global version:

    1
    
    jenv global 17.0.1
    
  • To set a local version for a specific directory:

    1
    
    jenv local 11.0.2
    
  • To set a version for the current shell session:

    1
    
    jenv shell 11.0.2
    

6. Verifying the Installation

To ensure jEnv is installed and working correctly, run:

1
jenv doctor

If jEnv is correctly loaded but no Java version is set, follow the steps above to add Java environments.

7. Switching Between Multiple Java Versions

If you need to manage multiple Java versions (e.g., for Android development with Java 8 and a server using Java 11), install and add them to jEnv, then switch versions as needed using the commands from the previous section.

8. Using jEnv with Android Studio

To use jEnv with Android Studio, you need to ensure that the correct Java path is set. Here’s how to configure them:

Setting Java Path in Android Studio

  1. Open Android Studio and go to File > Project Structure.
  2. In the Project Structure window, under SDK Location, set the JDK Location to the path managed by jEnv.
  3. Use the following command to get the path for the Java version you want to use with Android Studio:

    1
    
     jenv which java
    
  4. Copy the path output from the command and paste it into the JDK Location field.

☕ Support My Work

If you found this post helpful and want to support more content like this, you can buy me a coffee!

Your support helps me continue creating useful articles and tips for fellow developers. Thank you! 🙏

This post is licensed under CC BY 4.0 by the author.