Search code examples
javaantgitlab

Update gitlab JDK to JDK17 (VM doesn't have internet connection)


I am using Gitlab to build a Java tool using ant

The tool requires JDK 17, but ant JDK version is 11, and I'm trying to change it.
So I tried a lot of solutions using a remote repository or remote download site, but after some tries I found out that the VM used to build the tool is not connected to internet (trying to ping google or my IP address doesn't work).

So I tried to upload in the same package with the tool source code the JDK 17 (openjdk-17_linux-x64_bin.tar.gz) and install it there.
Here is the problem, I am not sure how to do this since I don't work with linux, but I tried almost everything on the internet.

Every of these commands are used in a .gitlab-ci.yml file, used for gitlab pipeline.

Here are some examples of what I've tried so far:


    - sudo cp /builds/project/openjdk-17_linux-x64_bin.tar.gz /usr/lib/jvm
    - sudo tar zxvf "/usr/lib/jvm/openjdk-17_linux-x64_bin.tar.gz" -C /usr/lib/jvm
    - echo "JAVA_HOME=/usr/lib/jvm/jdk-17" | sudo tee -a /etc/profile
    - echo "PATH=${PATH}:${HOME}/bin:${JAVA_HOME}/bin" | sudo tee -a /etc/profile
    - echo "export JAVA_HOME" | sudo tee -a /etc/profile
    - echo "export JRE_HOME" | sudo tee -a /etc/profile
    - echo "export PATH" | sudo tee -a /etc/profile
    - sudo cat /etc/profile
    - echo "JAVA_HOME=/usr/lib/jvm/jdk-17" | sudo tee -a /.bashrc
    - echo "PATH=${PATH}:${JAVA_HOME}/bin" | sudo tee -a /.bashrc
    - echo "JAVA_HOME='/usr/lib/jvm/jdk-17' | sudo tee -a /etc/environment"
    
    - export JAVA_HOME=/usr/lib/jvm/jdk-17
    - export PATH=$PATH:$JAVA_HOME/bin



After a lot of combinations of these commands the output of sudo update-alternatives --config java is still:

openjdk version "11.0.12" 2021-07-20
OpenJDK Runtime Environment (build 11.0.12+7-post-Debian-2deb10u1)
OpenJDK 64-Bit Server VM (build 11.0.12+7-post-Debian-2deb10u1, mixed mode, sharing)



But if I try /usr/lib/jvm/jdk-17/bin/java -version it prints 17.
What would be the solution of making the default Java version to be 17. (Also a solution for ant to use the JDK-17 without installing it would be great too, since I need the JDK-17 for ant)


Solution

  • I found a solution.

    
        - sudo cp jdk-17-linux-x64.tar.gz /usr/lib/jvm
        - sudo tar zxvf "/usr/lib/jvm/jdk-17-linux-x64.tar.gz" -C /usr/lib/jvm
        - sudo \cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/java-1.11.0-openjdk-amd64
        - sudo \cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/default-java
        - sudo \cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/java-11-openjdk-amd64
        - sudo \cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/openjdk-11
        - sudo update-alternatives --remove-all java
        - sudo update-alternatives --remove-all javac
        - sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-17/bin/java 1
    



    What I did here was to copy the JDK-17 content to all folders from /usr/lib/jvm folder. So even though the docker image uses JDK-11, I'm rewriting it using JDK-17 uploaded with the source code, and now the tool is built using JKD-17.
    PS: I know this is slower and not professional, but in my case, it's easier and more convinient than trying to get help from those who setup the docker container.