Skip to content

Find Java correctly when JAVA_TOOL_OPTIONS is set (fixes #523) - #561

Open
iacsha wants to merge 2 commits into
OpenIntegrationEngine:mainfrom
iacsha:fix/523-javatooloptions-version-parse
Open

iacsha wants to merge 2 commits into
OpenIntegrationEngine:mainfrom
iacsha:fix/523-javatooloptions-version-parse

Conversation

@iacsha

@iacsha iacsha commented Nov 4, 2026

Copy link
Copy Markdown

Find Java correctly when JAVA_TOOL_OPTIONS is set (fixes #523)

Problem

is_valid_java_version() in oieserver reads java -version and takes the first line:

version_output=$("$java_cmd" -version 2>&1)
major_version=$(echo "$version_output" | head -n 1 | cut -d '"' -f 2 | cut -d '.' -f 1)

When JAVA_TOOL_OPTIONS (or _JAVA_OPTIONS) is set, the JVM prints a Picked up JAVA_TOOL_OPTIONS: … banner to stderr before the version line. head -n 1 then
reads the banner instead of openjdk version "17.0.x", major_version is not numeric,
every candidate Java is rejected, and the server refuses to start.

Fix

Select the line that actually contains the quoted version string before cutting, so the
banner (or any other leading stderr noise) is ignored:

major_version=$(echo "$version_output" | grep -E 'version "' | head -n 1 | cut -d '"' -f 2 | cut -d '.' -f 1)

One line changed in server/basedir-includes/oieserver.

Verification

java -version under a JDK 17 runtime, with and without JAVA_TOOL_OPTIONS:

# JAVA_TOOL_OPTIONS=-Dfoo=bar
Picked up JAVA_TOOL_OPTIONS: -Dfoo=bar
openjdk version "17.0.15" 2025-04-15
...
OLD parse -> [Picked up JAVA_TOOL_OPTIONS: -Dfoo=bar]  INVALID (server would refuse to start)
NEW parse -> [17]  VALID

# no JAVA_TOOL_OPTIONS
openjdk version "17.0.15" 2025-04-15
...
OLD parse -> [17]  VALID
NEW parse -> [17]  VALID

The banner case now parses correctly, and the normal case is unchanged.

@mgaffigan

mgaffigan commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Can we simplify? We're doing a lot of pipelining here - perhaps an option on java or STDERR vs STDOUT. Or just a vanilla bash regex?

For example:

#!/bin/bash
if [[ $(java -version 2>&1) =~ ([0-9]+(\.[0-9_]+)+) ]]; then
    version="${BASH_REMATCH[1]}"
    echo "Java Version: $version"
fi

Seems to work with:

% ./jver.sh 
Java Version: 25.0.5
% JAVA_TOOL_OPTIONS="-Xms512m -Xmx2048m -XX:+UseG1GC" ./jver.sh
Java Version: 25.0.5
% 

Also: please make sure to sign your commit with a DCO.

@pacmano1

pacmano1 commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Ran this branch under eclipse-temurin:17: it starts with JAVA_TOOL_OPTIONS and with _JAVA_OPTIONS set, and jcmd shows the option reached the JVM. Main's script refuses with the same variable set. The fix works.

@mgaffigan on the regex: unanchored, it takes the first number.number in the output, and the banner comes first. With JAVA_TOOL_OPTIONS=-XX:MaxRAMPercentage=75.0 it reads "75" on Java 17 and on Java 11, so an old Java passes the check. With -Dotel.resource.attributes=service.version=1.4.2 it reads "1" and the launcher refuses Java 17 (tried it in the script). Your example has no dot, which is why it passed. Anchoring on the version line keeps the one-liner and passed everything I tried:

if [[ "$version_output" =~ version\ \"([0-9]+) ]]; then
    major_version="${BASH_REMATCH[1]}"
fi

@iacsha that plus the DCO sign-off (git commit --amend -s --no-edit, then git push --force-with-lease) and I'm good.

Co-authored with Claude Code.

@mgaffigan

Copy link
Copy Markdown
Contributor

@mgaffigan on the regex: unanchored

It was an example of methodology. Not something expected to be usable without edits.

@iacsha

iacsha commented Nov 4, 2026

Copy link
Copy Markdown
Author

Thanks both. @pacmano1 — appreciate the verification under both JAVA_TOOL_OPTIONS and _JAVA_OPTIONS, and good catch on the unanchored regex; that's the crux.

@mgaffigan — agreed, simplified. Dropped the pipeline for a single anchored bash regex (no grep/head/cut, no subshell):

local major_version=""
[[ "$version_output" =~ version\ \"([0-9]+) ]] && major_version="${BASH_REMATCH[1]}"

On the other two ideas: splitting streams doesn't help, because the Picked up … banner and java -version both go to stderr, so they're already on the same stream. And there's no portable version-only flag across the range (Java 8 has no --version). Anchoring on version " is what excludes the banner — exactly your point, @pacmano1: an unanchored [0-9]+\.[0-9]+ grabs the first match, which for JAVA_TOOL_OPTIONS=-XX:MaxRAMPercentage=75.0 is 75.0, not the version.

JAVA_TOOL_OPTIONS anchored unanchored
-XX:MaxRAMPercentage=75.0 17 75.0
(unset) 17 17.0

…ionEngine#523)

is_valid_java_version parsed java -version with head -n 1, so the Picked up JAVA_TOOL_OPTIONS banner printed to stderr was read instead of the version line and every candidate Java was rejected. Select the line containing the quoted version string before cutting.

Signed-off-by: iacsha <122627209+iacsha@users.noreply.github.com>
Replace the grep/head/cut pipeline with a bash regex anchored on the quoted version string. Anchoring on version " keeps the Picked up JAVA_TOOL_OPTIONS banner (and any other number in the output) from being read as the version.

Signed-off-by: iacsha <122627209+iacsha@users.noreply.github.com>
@iacsha
iacsha force-pushed the fix/523-javatooloptions-version-parse branch from 1ae8e94 to 182f4bd Compare November 6, 2026 19:47
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

Test Results

  141 files  ±0    141 suites  ±0   4m 7s ⏱️ + 1m 8s
  919 tests ±0    919 ✅ ±0  0 💤 ±0  0 ❌ ±0 
1 051 runs  ±0  1 045 ✅ ±0  6 💤 ±0  0 ❌ ±0 

Results for commit 182f4bd. ± Comparison against base commit 892e33f.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

oieserver.sh doesn't work when JAVA_TOOL_OPTIONS is set

3 participants