Code Monkey home page Code Monkey logo

jdk_switcher's Introduction

What JDK switcher is

This is a yet another Ubuntu/Debian-specific script that switches between multiple JDK versions (including those that may not have been installed via apt).

It is meant to be used in the travis-ci.org CI environment but may be useful in other environments, too.

Usage

Install one or more JDKs that register themselves with the update-java-alternatives tools by installing a .jinfo file under /usr/lib/jvm.

Then source jdk_switcher.sh in the repository root and use the jdk_switcher function it two arguments, a command and the JDK to use:

. ./jdk_switcher.sh

jdk_switcher home oraclejdk10

jdk_switcher  use openjdk7

jdk_switcher home ibmjdk8

Commands

  • use: switches active JDK (updates PATH alternatives and exports JAVA_HOME)
  • home: prints JAVA_HOME value for the specified JDK. Does not change anything in the environment.

JDK aliases

Supported aliases are:

  • oraclejdk10
  • oraclejdk8
  • oraclejdk7
  • openjdk7
  • openjdk6
  • ibmjdk8

How Does it Work?

The switcher uses update-java-alternatives (see also this intro) under the hood to update /etc/alternatives/* symlinks for java, javac, javap and other JDK tools. As such, the switcher itself primary handles aliasing of JDKs (update-java-alternatives aliases are too hard to remember) and updating JAVA_HOME value.

JAVA_HOME changes is the reason why jdk_switcher is implemented as a function.

Why was this tool necessary?

JAVA_HOME updates is the key reason for it to exist: update-java-alternatives and related Debian tools in general do a great job of managing alternatives but won't touch or even define JAVA_HOME. Even though most JVM ecosystem tools (from Leiningen to Elastic Search, HBase and Cassandra) will try hard to detect JAVA_HOME value but unfortunately, Maven 3 does it in a way that is heavily biased towards OpenJDK 6.

For travis-ci.org to support multiple JDKs for Clojure, Groovy, Java, Scala and JRuby, it is crucially important that all the tools we provision will use the JDK version specified for a build. We cannot let Maven always use OpenJDK 6.

License & Copyright

MIT LICENSE

Copyright (c) 2012-2019 Michael S. Klishin and Travis CI Development Team

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

jdk_switcher's People

Contributors

bitdeli-chef avatar brujoand avatar chandrams avatar gildegoma avatar k13gomez avatar lupino3 avatar meatballhat avatar michaelklishin avatar mpapis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jdk_switcher's Issues

Failed to switch to openjdk8

Faced with this issue on travis-ci
https://travis-ci.org/teamed/qulice/jobs/393331163#L460-L465

and reproduced locally

jdk_switcher use openjdk8
Switching to OpenJDK8 (java-1.8.0-openjdk-amd64), JAVA_HOME will be set to /usr/lib/jvm/java-8-openjdk-amd64
update-java-alternatives: plugin alternative does not exist: /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/IcedTeaPlugin.so

After that javac and java point to a completely wrong jdk - highest insalled (10.0.1 in my case).

Add support for AdoptOpenJDK images

AdoptOpenJDK (https://adoptopenjdk.net/index.html) is a vendor-neutral hosting platform for many highly-tested and supported JDKs. It is also the only place to get an equivalent of the IBM JDK past Java 8 (using the OpenJ9 VM).

A lot of good combinations could be added by supporting AdoptOpenJDK images including:

  • OpenJDK 8 w/ OpenJ9 (effectively IBM JDK 8)
  • OpenJDK 11 w/ OpenJ9
  • OpenJDK 12 w/ OpenJ9 OR Hotspot

These images are quite stable, and are also hosted as official images on DockerHub: https://hub.docker.com/_/adoptopenjdk

12.04 issue with openjdk6

Hey Michael,

It seems 12.04 uses a slightly different install path for openjdk6.

Instead of "/usr/lib/jvm/java-6-openjdk" it will use "/usr/lib/jvm/java-6-openjdk-i386", which means the jdk switcher won't work.

You rock!

Josh

Add support for the Azul Systems Zulu OpenJDK

Azul Systems offers for free a highly tested and certified OpenJDK that has an option for commercial support. The distribution is named Zulu. It is 100% compatible with the Oracle JDK. Arguably it is more compatible with the Oracle JDK than most Linux distributions' bundled OpenJDK packages.

Add IBM JDK

I'd love to be able to switch to the IBM JDK.

I'm working on getting time or getting someone to work on this from inside IBM, but primarily making this so that other people who might want the IBM JDK available can coordinate.

Add Open JDK 9

In TravisCI, the package is available (points at an ea), but the jdk switcher fails.

jdk:
  - oraclejdk9

addons:
  apt:
    packages:
      - oracle-java9-installer

Add a function 'set' to set the home of a JDK

Hi

On Ubuntu, the Oracle JDK7 is installed in /usr/java/jdk1.7.0_80/. Thus jdk_switcher won't work. Thus, adding a new function like set, to set the homeof a JDK will help in switching between differents installation directory.

jdk_switcher set oraclejdk7 '/usr/java/jdk1.7.0_80/'

Is it possible to switch patch releases of openjdk

The scenario is it needs to lock down the patch release of openjdk like v1.8.0_151 instead of keep using the latest?

E.g., the current openjdk verison from ubuntu xenial is v1.8.0_162 but a travis build environment requires v1.8.0_151.

Switching to openjdk8 sets java but not javac

I can reproduce this on a travis docker image (travisci/ci-garnet:packer-1512502276-986baf0):

travis@0db60250e227:~$ jdk_switcher use openjdk8
Switching to OpenJDK8 (java-1.8.0-openjdk-amd64), JAVA_HOME will be set to /usr/lib/jvm/java-8-openjdk-amd64
update-java-alternatives: plugin alternative does not exist: /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/IcedTeaPlugin.so
travis@0db60250e227:~$ java -version
Picked up _JAVA_OPTIONS: -Xmx2048m -Xms512m
openjdk version "1.8.0_141"
OpenJDK Runtime Environment (build 1.8.0_141-8u141-b15-3~14.04-b15)
OpenJDK 64-Bit Server VM (build 25.141-b15, mixed mode)
travis@0db60250e227:~$ javac -version
Picked up _JAVA_OPTIONS: -Xmx2048m -Xms512m
javac 9.0.1
travis@0db60250e227:~$ update-alternatives --config java
There are 4 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-9-oracle/bin/java              1091      auto mode
  1            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      manual mode
* 2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1069      manual mode
  3            /usr/lib/jvm/java-8-oracle/jre/bin/java          1081      manual mode
  4            /usr/lib/jvm/java-9-oracle/bin/java              1091      manual mode

Press enter to keep the current choice[*], or type selection number: ^C
travis@0db60250e227:~$ update-alternatives --config javac
There are 4 choices for the alternative javac (providing /usr/bin/javac).

  Selection    Path                                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-9-oracle/bin/javac          1091      auto mode
  1            /usr/lib/jvm/java-7-openjdk-amd64/bin/javac   1071      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/bin/javac   1069      manual mode
  3            /usr/lib/jvm/java-8-oracle/bin/javac          1081      manual mode
  4            /usr/lib/jvm/java-9-oracle/bin/javac          1091      manual mode

Press enter to keep the current choice[*], or type selection number: ^C

This makes builds fail as javac from oracle jdk 9 is used to compile class files which then cannot be run with java from openjdk 8 (due to incompatible class file format)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.