Using different Java versions for agents and jobs

Hi!

I have a Jenkins setup in my Kubernetes cluster, but my agents are outside. I connect agents using SSH, but I have one problem with Java.

For my project I must use java 8, however new Jenkins only supports 11 and 17. So in order to launch my node agent and connect, I must remove environment variable from the node settings, start my node, and then add JAVA_HOME jdk1.8.0_151 back to node environment variables. This is very inconvenient, since I have to manually do these steps if Jenkins or the agent get restarted.

I know I could add jdk1.8.0_151 environment variable to my job, but I think this is not very correct way, since I might have several nodes with different path to java8.

So, what may be the correct approach here?

You can configure the full path to java in the config (under advanced settings). This way you avoid that JAVA_HOME is used or java is taken from the path.

Thank you! I didn’t notice this field. It looks like the agent starts fine even with the env variable now.

@mawinter69 , our jenkins server running on Java 17, and agents on java 8, added java path in agents advanced settings, getting
Verified agent jar. No update is necessary.
Expanded the channel window size to 4MB
[01/29/25 16:32:48] [SSH] Starting agent process: cd “/var/lib/Jenkins” && /usr/java/jdk1.8.0_321-amd64/bin/java -jar remoting.jar -workDir /var/lib/Jenkins -jar-cache /var/lib/Jenkins/remoting/jarCache
Error: A JNI error has occurred, please check your installation and try again
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 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)

The agent process must run with Java 11 or Java 17 (depends on what version of Jenkins you’re using).
But this should not stop you from having multiple Java versions installed on the agent and use whatever java version you need for the build.

@mawinter69 , our source code still using java 8

Then use java8 to build your sources but the agent process must run a newer java version.
It would help if you tell us how you’re building your sources. When you use the Maven job type then it is strongly advised to move away from it.

we are using gradle for build our jars

So where is the problem?
Not familiar with gradle but you run this is a separate process I assume. Does it rely on JAVA_HOME being set? Then set JAVA_HOME to the jdk you need.