# Multiple Java Versions

If you use multiple versions of Java in your development environment, perhaps you wished there was a better way to switch from one to the other, instead of remembering to set the `JAVA_HOME` environment variable.

---

Forgot to set the `JAVA_HOME` environment variable? If you use multiple versions of Java in your development environment, perhaps you wished there was a better way to switch from one to the other, instead of remembering to set the `JAVA_HOME` environment variable. Introducing [jEnv](http://www.jenv.be/):

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">jEnv is a command line tool to help you forget how to set the <code>JAVA_HOME</code> environment variable.</div>
</div>

## Install

In the following sections, we describe the installation procedure of jEnv on Linux and macOS environments.

### Linux

Simply clone the jEnv repository from GitHub:

```bash
git clone https://github.com/gcuisinier/jenv.git ~/.jenv
```

### macOS

We need the [Homebrew](https://brew.sh/) package manager for macOS:

```bash
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
```

Install jEnv using `brew`:

```bash
brew install jenv
```

## Configure

Update your command line environment with jEnv-specific configuration:

```bash
export JAVA_HOME="$HOME/.jenv/versions/`jenv version-name`"
alias jenv_set_java_home='export JAVA_HOME="$HOME/.jenv/versions/`jenv version-name`"'
```

Next, you'll need to add the JAVA home directory paths to your jEnv environment. To list all the JVM versions and architectures in your environment, run:

```bash
/usr/libexec/java_home -V
```

You may get an output similar to (e.g., on macOS):

```bash
Matching Java Virtual Machines (3):
    11.0.1, x86_64:	"OpenJDK 11.0.1"	/Library/Java/JavaVirtualMachines/openjdk-11.0.1.jdk/Contents/Home
    1.8.0_192, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
    1.8.0_101, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home
```

Based on the JVM list above you may add the JAVA home directory paths to your jEnv environment, e.g.:

```bash
jenv add /Library/Java/JavaVirtualMachines/openjdk-11.0.1.jdk/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
```

Now your jEnv environment is ready to manage the various JVM versions in your development environment.

```bash
jenv versions
```

The aforementioned command would yield an output similar to:

```bash
  system
  1.8
  1.8.0.192
  11.0
  11.0.1
  openjdk64-11.0.1
  oracle64-1.8.0.192
```

You can set specific versions of JVM per workspace directory. Assuming you have a directory with a Java code base requiring JVM 1.8, you can switch to that directory and set the JVM version of choice, e.g.:

```bash
jenv local 1.8
```

You may still choose a default `global` version of JVM on your system, e.g., `11`:

```bash
jenv global 11
```

Happy Java coding!

## **Resources**

[http://www.jenv.be/](http://www.jenv.be/)
