开发者

JVM version manager

Is there Ruby Version Manager equivalent for the Java world?

I'm looking for tool which allow me to easily download and install a new JVMs and switch between them. For example:

jvm install <versio开发者_运维技巧n>
jvm list //will list installed JVMs on my system
jvm use jdk1.6 //will switch my env to jdk 1.6 version, etc.


http://www.jenv.be/ will allow this type of control.


SDKMAN! is a similar tool for the Java ecosystem. Supports various Java versions, Scala, Clojure, Kotlin, Groovy, and build tools like Maven and Gradle.

Works on Mac and Linux, with some mentions of support for Windows depending on how hard you are willing to try :)


If you use Ubuntu you can specify which JVM you want to use via command (works only for JVM installed from apt-get or aptitude)

sudo update-alternatives --config java

Or by setting JAVA_HOME. Here is good tutorial:

http://vietpad.sourceforge.net/javaonlinux.html


For the sake of completeness, there are two more - jabba (of which I am the author; written in Go and designed after nvm/gvm/rvm) and jenv (not to confuse with jenv.be; doesn't support installation from oracle but can install from a custom zip).


With JVMs, if you need to switch between them you just need to use a batch file (or powershell script) to manage the classpath and JVM path. You don't need to rely on the system default JVM path and instead just allow your app to point to whatever JVM you like by changing classpath and JVM path environment in the shell that runs the JVM.

For programs that are getting Java location from the Registry, in theory you could use a batch script to update that also.

In this respect Java is way easier than "Ruby version manager".


As it is not (yet) in the list of possibilities, there's also asdf.
asdf does not only provide version management for java, it has plugins for ~400 different languages and tools by default, you can find more on github, or create your own.

Here is an example how to setup a new install (you can also install completion so you don't have to list the versions first). The java plugin is added, a specific version (there are versions for adoptopenjdk, corretto, dragonwell, graalvm, liberica, mandrel, microsoft, openjdk, oracle, sapmachine, semeru, temurin, trava, zulu) is installed and configured to be the global (or local version) to use:

asdf plugin-add java          # Add java Plugin
asdf list-all java            # List all available java versions
asdf install java openjdk-18  # Install specific jdk version
asdf install java openjdk-17  # Install another jdk version
asdf global java openjdk-18   # Set the global jdk version
asdf local java openjdk-17    # Set the local version for calls from the current directory

asdf uses a file in $HOME/.tool-versions to configure the global selected version. If you call any tool in a directory that has a .tool-versions file with a different version, that one is used (defined with asdf local …).


The trick is to use update-java-alternatives (from the java-common package). The update-alternatives command will not update every one of the symbolic links for various java /bin executables, which is why update-java-alternatives is better.

So to go for OpenJDK 6 to 7, use update-java-alternatives -l to get a list of Java alternatives and then used sudo update-java-alternatives -s java-1.7.0-openjdk-amd64 to switch the JDK.

CAVEAT: The command above might throw the following errors,

update-alternatives: error: no alternatives for mozilla-javaplugin.so.
update-java-alternatives: plugin alternative does not exist: 
  /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/IcedTeaPlugin.so

This is because the openjdk plugin is not installed by default. To fix run sudo apt-get install icedtea-7-plugin and rerun update-java-alternatives.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜