Not able to use different java version when using ECS plugin for dynamic agent setup

Jenkins setup: I have my Jenkins controller setup on ECS cluster and also using ECS plugin to provision Jenkins docker agents on ECS.

For Jenkins agents, i am using jenkins/inbound-agent:latest-alpine-jdk21 docker image and the ECS plugin uses JNLP for connection b/w controller and the agent. I able to successfully run jobs on docker containers on ECS.

The issue i am facing is with the Java version, some of my jobs needs Java 1.8 version but the jenkins agent needed Java 21. I even created my own custom agent docker image with Java 8 installed and added in PATH and Java 21 removed from PATH and made agent to use Java 21 binary via JENKINS_JAVA_BIN. But still the jobs always uses Java 21, i even verified with which java and echo $PATH commands and both of them shows java 1.8 only but still java -version and mvn -version always prints Java 21.

I want to know is it even possible to change the Java version when using JNLP for the agents, i think since agent connects through JNLP which creates a Java process with Java 21, so that’s why i not able to use Java 1.8 even its in the PATH and default Java.

Below is the output of my diagnosis from job output:

+ which java
/usr/lib/jvm/java-1.8-openjdk/bin/java
+ echo /usr/lib/jvm/java-1.8-openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/maven/bin:/opt/ant/bin:/opt/gradle/bin
/usr/lib/jvm/java-1.8-openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/maven/bin:/opt/ant/bin:/opt/gradle/bin
+ echo /usr/lib/jvm/java-1.8-openjdk
/usr/lib/jvm/java-1.8-openjdk
+ java -version
openjdk version "21.0.8" 2025-07-15 LTS
OpenJDK Runtime Environment Temurin-21.0.8+9 (build 21.0.8+9-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.8+9 (build 21.0.8+9-LTS, mixed mode)
+ mvn -version
Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937)
Maven home: /opt/maven
Java version: 21.0.8, vendor: Eclipse Adoptium, runtime: /opt/java/jdk21
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "6.1.156-177.286.amzn2023.x86_64", arch: "amd64", family: "unix"
+ echo /home/jenkins
/home/jenkins
+ type java
java is /usr/lib/jvm/java-1.8-openjdk/bin/java

see Remoting Agent with Java 17 & Alpine causing incorrect Java version to be picked up

Thank you @mawinter69 for the quick response. I was stuck at it from past few days and its solved now, for the quick fix, i moved to use ubuntu image instead.

I am now getting another issue with “Maven Project” type jobs, i not able to change the Java version, my jobs needs Java 8 which is present in the agent and added in the PATH but still it uses Java 21 and if i made Java 8 as default, the job failed with this error.

19:20:47 Exception in thread "main" java.lang.UnsupportedClassVersionError: hudson/remoting/Launcher has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 52.0

Can you please help me to fix this as well or point to something. It would be greatly appreciated. Thanks.

Move away from the maven project type. On Maven Integration plugin page the problems of that job type are explained.
You might try to still use Java 21 but explicitly set the source and target versions for the compiler in your pom. But I don’t know if that works for Java 8.

1 Like

There are some cases where you cannot compile Java 8 code with Java 21.

We have customized Jenkins agents with Java 8, 17, and 21 installed; we’ve just moved most of our code from Java 8 to Java 21 and the Agents themselves are running with Java 17. The agent only has Java 17 on the path.

To get around incompatibilities, I have a custom pipeline build step that calls the build tool (we use Maven and Gradle) with the required JDK. We do not put all of the JDKs on the path due to how they behave, but use a withEnv(PATH+JAVA_HOME="${tool ‘java8’}) call to get the specific JDK at the front of the path well after the agent has started up.

Eventually we’ll get our environment coalesced on Java 21 or later and I won’t have to resort to such trickery to ensure builds.

1 Like

Thanks for the guidance @mawinter69, I converted all my “Maven Project” type jobs to Pipeline type jobs.