Search code examples
dockerjava-8dockerfilejava-11

I am trying to update my dockerfile from oracle Java 8 to OpenJdk 11. I am facing problems while updating the aalternatives for java compiler


I am completely new to docker. We are updating all our servers to Java 11. Below is the docker file we have which contains Java 8.

FROM ubuntu:latest

ENV DEBIAN_FRONTEND noninteractive

# JAVA VERSION
ENV VERSION 8

# config for home directory
ENV JAVA_HOME /usr/lib/jvm/java-${VERSION}-oracle

ENV DEBIAN_FRONTEND noninteractive

# JDK 8
RUN \
  echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | debconf-set-selections && \
  apt-get update && \
  apt-get install -y --no-install-recommends software-properties-common ca-certificates net-tools curl unzip lsof wget git openssh-client && \
  add-apt-repository -y ppa:webupd8team/java && \
  apt-get update && \
  apt-get install -y oracle-java8-set-default oracle-java8-installer && \
  rm -rf /var/lib/apt/lists/* && \
  rm -rf /var/cache/oracle-jdk8-installer

# set jre as default java
RUN update-alternatives --install "/usr/bin/java" "java" "${JRE_HOME}/bin/java" 1 \
    && update-alternatives --install "/usr/bin/javac" "javac" "${JAVA_HOME}/bin/javac" 1 \
    && update-alternatives --set java "${JRE_HOME}/bin/java" \
    && update-alternatives --set javac "${JAVA_HOME}/bin/javac"

I updated my docker file to

FROM ubuntu:latest

ENV DEBIAN_FRONTEND noninteractive

# JAVA VERSION
ENV VERSION 11

# config for home directory
ENV JAVA_HOME /usr/lib/jvm/java-${VERSION}-openjdk-amd64

ENV DEBIAN_FRONTEND noninteractive

# JDK 11
RUN apt-get update && \
    apt-get install -y openjdk-11-jdk && \
    apt-get install -y ant && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* && \
    rm -rf /var/cache/oracle-jdk11-installer;

# set jre as default java
RUN update-alternatives --install "/usr/bin/java" "java" "${JRE_HOME}/bin/java" 1 \
    && update-alternatives --install "/usr/bin/javac" "javac" "${JAVA_HOME}/bin/javac" 1 \
    && update-alternatives --set java "${JRE_HOME}/bin/java" \
    && update-alternatives --set javac "${JAVA_HOME}/bin/javac"

I am getting error as

0.285 update-alternatives: error: alternative path /usr/lib/jvm/java-11-openjdk-amd64/jre/bin/java doesn't exist

Why alternatives are not getting updated? What wrong i am doing here?


Solution

  • There is no folder jre in the specified directory for Java 11+. In my local installation it is

    $ ls /usr/lib/jvm/java-11-openjdk-amd64/
    bin/     conf/    docs/    legal/   lib/     man/     release
    

    See also this answer for why you don't need JRE_HOME anymore.

    Change the last line in your dockerfile to

    RUN update-alternatives --install "/usr/bin/javac" "javac" "${JAVA_HOME}/bin/javac" 1 \
        && update-alternatives --set javac "${JAVA_HOME}/bin/javac"
    

    i.e. remove the lines with JRE_HOME.