Java installs multiple times during Freestyle builds

I run some projects that use Oracle Java 8 and starting with 391 the installer only allows one version of the JDK to be on the machine. We have different apps currently using/testing different versions of the JDK so we used to keep 3-4 versions available and pick and choose them as normal in our builds (both freestyle and pipeline).

After 391 came out I tried my hand at making bat and sh scripts to install the requested version of the JDK on the fly, which seemed to work, but it also calls the script MANY times (40-50 times in some recent testing) in freestyle builds. Works as expected, once, in Pipeline builds.

I’ve if’d the scripts so they don’t try to actually install the JDK that many times, but I can’t figure out why they are being called so much. I am also not sure that all these calls are exiting properly as I’ve noticed the JDK version being changed when no builds are even running unless I rename the bat files (still testing to see if the sh scripts also have the same issue).

Jenkins setup:

Jenkins: 2.444
OS: Windows Server 2016 - 10.0
Java: 11.0.21 - Oracle Corporation (Java HotSpot™ 64-Bit Server VM)

analysis-model-api:11.15.0
ant:497.v94e7d9fffa_b_9
antisamy-markup-formatter:162.v0e6ec0fcfcf6
apache-httpcomponents-client-4-api:4.5.14-208.v438351942757
authentication-tokens:1.53.v1c90fd9191a_b_
authorize-project:1.7.1
badge:1.9.1
bootstrap4-api:4.6.0-6
bootstrap5-api:5.3.2-3
bouncycastle-api:2.30.1.77-225.v26ea_c9455fd9
branch-api:2.1148.vce12cfcdf090
build-blocker-plugin:1.7.9
build-history-metrics-plugin:112.v476124de7dfc
build-metrics:1.3
build-name-setter:2.4.1
built-on-column:1.4
caffeine-api:3.1.8-133.v17b_1ff2e0599
checks-api:2.0.2
cloudbees-folder:6.919.va_e17e72383f5
command-launcher:107.v773860566e2e
commons-httpclient3-api:3.1-3
commons-lang3-api:3.13.0-62.v7d18e55f51e2
commons-text-api:1.11.0-95.v22a_d30ee5d36
conditional-buildstep:1.4.3
config-file-provider:968.ve1ca_eb_913f8c
content-replace:1.8.2
convert-to-pipeline:1.0
copyartifact:722.v0662a_9b_e22a_c
credentials:1319.v7eb_51b_3a_c97b_
credentials-binding:657.v2b_19db_7d6e6d
custom-checkbox-parameter:1.4
data-tables-api:1.13.8-2
disk-usage:1.2
display-url-api:2.200.vb_9327d658781
durable-task:550.v0930093c4b_a_6
echarts-api:5.4.3-2
editable-choice:71.v02a291ebbe45
email-ext:2.104
envinject:2.908.v66a_774b_31d93
envinject-api:1.199.v3ce31253ed13
extensible-choice-parameter:1.8.1
external-monitor-job:215.v2e88e894db_f8
flaky-test-handler:1.2.3
flexible-publish:0.16.1
forensics-api:2.3.0
git:5.2.1
git-changelog:3.38
git-client:4.6.0
git-parameter:0.9.19
git-server:114.v068a_c7cc2574
github:1.38.0
github-api:1.318-461.v7a_c09c9fa_d63
github-pullrequest:0.6.1
global-build-stats:288.vb_2c4a_0f138b_b_
global-pre-script:20.02
greenballs:1.15.1
groovy:457.v99900cb_85593
groovy-postbuild:228.vcdb_cf7265066
gson-api:2.10.1-15.v0d99f670e0a_7
h2-api:11.1.4.199-12.v9f4244395f7a_
handlebars:3.0.8
http_request:1.18
instance-identity:185.v303dc7c645f9
ionicons-api:56.v1b_1c8c49374e
jackson2-api:2.16.1-373.ve709c6871598
javadoc:243.vb_b_503b_b_45537
javax-activation-api:1.2.0-6
jaxb:2.3.9-1
jdk-tool:73.vddf737284550
jenkins-multijob-plugin:623.v03401733c9a_9
jgiven:0.15.1
jnr-posix-api:3.1.18-1
jobConfigHistory:1229.v3039470161a_d
joda-time-api:2.12.7-29.v5a_b_e3a_82269a_
jquery:1.12.4-1
jquery-detached:1.2.1
jquery3-api:3.7.1-1
jsch:0.2.16-86.v42e010d9484b_
json-path-api:2.9.0-33.v2527142f2e1d
junit:1259.v65ffcef24a_88
junit-attachments:205.vc0677977deb_0
lockable-resources:1232.v512d6c434eb_d
log-parser:2.3.3
mailer:463.vedf8358e006b_
matrix-auth:3.2.1
matrix-project:822.824.v14451b_c0fd42
maven-plugin:3.23
metrics:4.2.21-449.v6960d7c54c69
metrics-diskusage:3.0.0
mina-sshd-api-common:2.12.0-90.v9f7fb_9fa_3d3b_
mina-sshd-api-core:2.12.0-90.v9f7fb_9fa_3d3b_
momentjs:1.1.1
monitoring:1.98.0
msbuild:1.30
multibranch-build-strategy-extension:48.v3dc306525d0c
multiple-scms:0.8
multiselect-parameter:1.7
muuri-api:0.9.5-3
nodelabelparameter:1.12.0
nuget:1.1
okhttp-api:4.11.0-172.vda_da_1feeb_c6e
pam-auth:1.10
parameterized-scheduler:262.v00f3d90585cc
parameterized-trigger:787.v665fcf2a_830b_
pipeline-build-step:540.vb_e8849e1a_b_d8
pipeline-graph-analysis:202.va_d268e64deb_3
pipeline-groovy-lib:704.vc58b_8890a_384
pipeline-input-step:491.vb_07d21da_1a_fb_
pipeline-maven:1376.v18876d10ce9c
pipeline-maven-api:1376.v18876d10ce9c
pipeline-milestone-step:111.v449306f708b_7
pipeline-model-api:2.2175.v76a_fff0a_2618
pipeline-model-definition:2.2175.v76a_fff0a_2618
pipeline-model-extensions:2.2175.v76a_fff0a_2618
pipeline-multibranch-defaults:2.1
pipeline-rest-api:2.34
pipeline-stage-step:305.ve96d0205c1c6
pipeline-stage-tags-metadata:2.2175.v76a_fff0a_2618
pipeline-stage-view:2.34
pipeline-utility-steps:2.16.2
plain-credentials:143.v1b_df8b_d3b_e48
plugin-usage-plugin:4.2
plugin-util-api:3.8.0
popper-api:1.16.1-3
popper2-api:2.11.6-4
postbuild-task:1.9
postbuildscript:3.2.0-550.v88192b_d3e922
powershell:2.1
preSCMbuildstep:71.v1f2990a_37e27
prism-api:1.29.0-10
pull-request-monitoring:1.7.8
role-strategy:689.v731678c3e0eb_
run-condition:1.7
scm-api:683.vb_16722fb_b_80b_
script-security:1321.va_73c0795b_923
select2-api:4.0.13-8
shared-objects:0.44
skip-certificate-check:1.1
snakeyaml-api:2.2-111.vc6598e30cc65
ssh-credentials:308.ve4497b_ccd8f4
sshd:3.322.v159e91f6a_550
structs:337.v1b_04ea_4df7c8
timestamper:1.26
token-macro:400.v35420b_922dcb_
trilead-api:2.133.vfb_8a_7b_9c5dd1
uno-choice:2.8.1
variant:60.v7290fc0eb_b_cd
versioncolumn:243.vda_c20eea_a_8a_f
versionnumber:1.11
view-job-filters:369.ve0513a_a_f5524
warnings-ng:10.7.0
windows-slaves:1.8.1
workflow-aggregator:596.v8c21c963d92d
workflow-api:1291.v51fd2a_625da_7
workflow-basic-steps:1042.ve7b_140c4a_e0c
workflow-cps:3853.vb_a_490d892963
workflow-durable-task-step:1331.vc8c2fed35334
workflow-job:1400.v7fd111b_ec82f
workflow-multibranch:773.vc4fe1378f1d5
workflow-scm-step:415.v434365564324
workflow-step-api:657.v03b_e8115821b_
workflow-support:865.v43e78cc44e0d

are you adding java installation step in freestyle job as a separate build step?
How many times the freestyle jobs runs?

We cannot predict cause as per the given information by you.

No, I am using the Jenkins Tools selection of “Install Automatically” in the JDK section. The options for “Install Automatically” are:

  • Extract zip/tar.gz
  • Install Oracle … from website
  • Run Batch Command
  • Run Sh command

A single run of the freestyle build will run either the batch or sh commands multiple times in the course of the run, usually multiple times in individual build steps (but not every build step).

Each time it runs the console shows for example:

**15:06:55** New run name is '#30.358.9.9997' 
**15:06:55** [jdk1.8.0_391_auto] $ sh -e C:\J\tools\hudson.model.JDK\jdk1.8.0_391_auto\hudson13515112110805072328.sh 
**15:06:57** [jdk1.8.0_391_auto] $ sh -e C:\J\tools\hudson.model.JDK\jdk1.8.0_391_auto\hudson1089906654796401101.sh 
**15:06:58** [jdk1.8.0_391_auto] $ sh -e C:\J\tools\hudson.model.JDK\jdk1.8.0_391_auto\hudson9135652287968545959.sh 
**15:07:00** Archiving artifacts 
**15:09:05** Recording fingerprints

I likely could resolve this by putting the JDK installation in my own build step, but this is the process baked into Jenkins and it is not functioning as I would expect in freestyle builds.

Any reason why you stick to Oracle JDK?
Unless you’re from Oracle I see no good reason to use it, also from a licensing perspective.
I assume before that you didn’t use the tool installer feature of Jenkins. What I noticed is that the tool installer is always installing the jdk (if not already done before) in a freestyle job even when the job is doing nothing, when envinject plugin is installed.
The jdk is installed when there is only a single jdk configured, but not when there are multiple jdks configured (see jenkins/core/src/main/java/jenkins/model/Jenkins.java at db61f04af8e553dc55d2cb2fa18fa5581dab4310 · jenkinsci/jenkins · GitHub)
I could image that other plugins also check for java tools and just try to install it when only a single jdk is configured. You might want to try to configure a second dummy jdk to avoid this.

Prior to Java 8 r391 the installer would let multiple versions live on the build clients so we had 3 versions installed without issue. 391 uninstalls all the older versions when it runs and also defaults the JDK and JRE install directories to jdk1.8 instead of the old jdk1.8.0_nnn format. We support several legacy project that use different versions of Java 8, as well as developing on newer versions as needed for security concerns.

We have 4 JDK installs in Tools, plus each has an “auto” variant that is trying to handle the auto install, so a total of 8 JDK option in the freestyle dropdown.

I can work around this issue in many different ways, but I can’t determine if I am using the JDK Install Automatically process incorrectly or if it just doesn’t work properly in freestyle builds. Our newer projects use pipeline and it works properly/once, but refactoring older projects just to reduce 40+ noisy lines in the console does not meet juice:squeeze.