Somewhere between Dev and Ops

cat /dev/ops | grep gc

Introducing jenv, a command line Java/JDKs Manager

| Commentaires

Introduction

As a developer, it is sometimes necessary to have multiple JDKs installed on the same development machine in order to test the latest JDK 8, or to use a proprietary JDK like the IBM one. But even if there are several installed, when you type java, the first one “java” on the PATH will be used.

With Linux, a tool exists : Alternative. This one creates a symbolic link that points to the JAVA_HOME you specified. When you want to change of JDK, Alternative will change this link. If Alternative is very usefull for managing JDK on a server, it does not answer the need for a development station.

To fit these needs, one way is to manage manually the JAVA_HOME and PATH environment variables. To simplify the switching, there is some scripts that may help, like this one Alternative.

However, these scripts help the switch but does not automate it. You always have to type “setjdk” when you change of project.

When you look at the ruby worl, there is a very handy tool : rbenv. This allows facilities to manage different versions of ruby and choose per project which is the version that you want to use. And once you setup it, you only have to type ruby, and rbenv will use the selected version automatically and transparently.

After some research, I have found no equivalent tool for Java and I took the initiative to create it : jenv

Get started

Installation

For now, the installation is manual. You simple clone the Github project into $HOME/.jenv directory.

1
git clone https://github.com/hikage/jenv.git ~/.jenv

Next, you have to activate it by inserting this commands into the shell init script ( .bash_profile, .zshrc)

1
2
export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)

Setup

One installed, you have to setup one or more JDK by using jenv add command. This one take one parameter : the path to a valid JAVA_HOME directory

1
2
$ jenv add /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
$ jenv add oracle-1.7.0 /Library/Java/JavaVirtualMachines/jdk17011.jdk/Contents/Home

Through jenv versions command, you can list the recognized JDKs.

1
2
3
4
$ jenv versions
* system (set by /Users/hikage/.jenv/version)
  oracle-1.6.0.39
  oracle64-1.7.0.11

The asterisk * denotes the JDK currently used. With no specific configuration it’s the “system” JDK to be used, that means the first found on the PATH. It is possible to change the default version with jenv global <alias name>.

1
$ jenv global oracle64-1.7.0.11

You can hit “TAB” after jenv global to have autocompletion for the alias name. The jenv versions now show that the oracle64-1.7.0.11 will be used.

To configure a JDK per directory/project, you can use jenv local <alias name>

1
2
3
4
5
6
7
$ pwd
/Users/hikage/demo
$ jenv local oracle-1.6.0.39
$ jenv versions
  system
* oracle-1.6.0.39 (set by /Users/hikage/demo/.java-version)
  oracle64-1.7.0.11

To work, jenv store the java version to be used into the .java-version file in the directory. If no .java-version file found, jenv will look into the parent directory, and so on. If none is found, then jenv will switch to the default JDK ( setup by jenv global )

Jenv also has plugins to use the correct version of java with other tools: * Maven * Ant * Gradle * Groovy * Scala * SBT * Play

In addition to JDK management, jenv can also configure JVM paremeters per project :

1
2
$ jenv local-options "-Xms128m -Xmx1024"
$ jenv global-options "-Xms128m -Xmx1024"

And they are also exported via the right environment variables to be used by Maven (MAVEN_OPTS) Gradle (GRADLE_OPTS) or Ant (ANT_OPTS).

Jenv is young, and will be improved

Jenv is very recent, and provides only simple functionalities that I needed for the moment. It is not yet complete, and has to be improved.

The project is on GitHub, so feel free to fork and contribute ideas (by pull requests or issues).

Comments