jEnv is the tool for quick and easy management of you java versions. Which is pretty cool.

If you have ever played with Ruby you might be familiar with rbenv. When I was doing ruby this was the standard way to set up your environment. With rbenv you can easily install different ruby versions with no conflicts and manage these versions.

When I was doing Kafka stuff and preparing my sample projects, I thought I would do it in Java 9, because why not. Who wouldn’t want to create a list like this:

List<String> strings = List.of("one", "two", "three");

So I did it. But then I wanted to use some other project, based on Java 8. Tests started to fail in a very weird way. Tests that were definitely passing. It turned out that Java 9 was to blame.

Then my colleague told me about jEnv and because I’ve never heard about it, now it’s good opportunity to pass this cool thing to the world.

Instructions on project’s website are good - go there, as an example on Mac with zsh you write in terminal:

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

Then you need to tell jEnv where you keep all your Javas, in my case:

$ jenv add /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home

That’s the end of set up. When you run jenv versions you will see your available java versions:

  system
  1.7
  1.7.0.80
  1.8
* 1.8.0.40 (set by /Users/michal/.jenv/version)
  9.0
  9.0.1

* means version you are currently using. You can also check that using jenv version (without s).

You change your current version by $ jenv global VERSION_NAME e.g.

$ jenv global 9.0

You can also set java version to use in current directory or shell by replacing global with local and shell respectively.

Awesome, now you can change Java version whenever you like with one simple command.

Back to the project - we switch to Java 8, does it work now? Of course not! Because it’s Maven building the project, we also need to make sure that Maven is using Java version that jEnv is setting. Fortunately, it’s very easy, jEnv has plugins for it. You can show all plugins with

$ jenv plugins
ant
export
golo
gradle
grails
groovy
lein
maven
sbt
scala
springboot
vlt

and enable desired (in this case, maven) plugin with

$ jenv enable-plugin maven

Now it really works and you can easily have projects with different java version without hassle.

Kudos to gcuisinier for great project!