Use Java 8 within GitHub CodeSpaces

Currently, Java 11 is installed on GitHub CodeSpaces and you can’t change that as easily as on your local machine with commands like:

sudo apt-get update
sudo apt-get install openjdk-8-jdk
sudo update-java-alternatives --set java-1.8.0-openjdk-amd64

Also, setting the JAVA_HOME variable didn’t have an influence on the Java version used.

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64

Still java -version gives me the output:

openjdk version "11.0.14.1" 2022-02-08 LTS
OpenJDK Runtime Environment Microsoft-31205 (build 11.0.14.1+1-LTS)
OpenJDK 64-Bit Server VM Microsoft-31205 (build 11.0.14.1+1-LTS, mixed mode)

The good thing is, that you can force a Java version for a specific Maven task as well. To do so just add JAVA_HOME={PATH_TO_THE_JDK_TO_USE} in front of your Maven command. So in my case, it’s:

JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64 mvn install

Cheat Sheet

sudo apt-get update
sudo apt-get install openjdk-8-jdk
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64 mvn install

Published by Sandra Ahlgrimm

Cloud Advocate @ Azure with a passion for Java and containers

Leave a comment