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
For now, the installation is manual. You simple clone the Github project into $HOME/.jenv directory.
Next, you have to activate it by inserting this commands into the shell init script ( .bash_profile, .zshrc)
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
jenv versions command, you can list the recognized JDKs.
1 2 3 4
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>.
You can hit “TAB” after
jenv global to have autocompletion for the alias name.
jenv versions now show that the oracle64-188.8.131.52 will be used.
To configure a JDK per directory/project, you can use
jenv local <alias name>
1 2 3 4 5 6 7
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 :
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).