Skip to content

Commit 8e09aba

Browse files
committed
Fix JDK extraction to handle different archive structures
Improve JDK extraction logic to properly handle different archive structures across platforms: - macOS JDKs often have Contents/Home/ directory structure - Linux/Windows JDKs may have different nesting levels - SDKMAN JDKs can vary by vendor and platform Changes: - Extract to temporary location first - Search for actual JDK root (where bin/java exists) up to 3 levels deep - Move the correct JDK root to final location - This handles macOS Contents/Home structure and other variations Fixes: JAVA_HOME pointing to wrong directory level causing 'JAVA_HOME/bin/java does not exist' errors on macOS
1 parent 2b5f3a0 commit 8e09aba

File tree

1 file changed

+28
-14
lines changed
  • maven-wrapper-distribution/src/resources

1 file changed

+28
-14
lines changed

maven-wrapper-distribution/src/resources/only-mvnw

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -442,26 +442,18 @@ install_jdk() {
442442
fi
443443
fi
444444

445-
# Extract JDK
446-
verbose "Extracting JDK to: $jdk_home"
447-
mkdir -p "$jdk_home"
445+
# Extract JDK to temporary location first
446+
local jdk_extract_dir="$jdk_tmp_dir/extract"
447+
mkdir -p "$jdk_extract_dir"
448+
verbose "Extracting JDK archive to temporary location"
448449

449450
case "$jdk_filename" in
450451
*.tar.gz | *.tgz)
451-
tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$jdk_file" -C "$jdk_home" --strip-components=1 || die "failed to extract JDK tar.gz"
452+
tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$jdk_file" -C "$jdk_extract_dir" || die "failed to extract JDK tar.gz"
452453
;;
453454
*.zip)
454455
if command -v unzip >/dev/null; then
455-
# Extract and find the JDK directory
456-
unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$jdk_file" -d "$jdk_tmp_dir" || die "failed to extract JDK zip"
457-
# Find the JDK directory and move its contents
458-
local jdk_extracted_dir
459-
jdk_extracted_dir="$(find "$jdk_tmp_dir" -maxdepth 1 -type d -name "*jdk*" | head -1)"
460-
if [ -n "$jdk_extracted_dir" ] && [ -d "$jdk_extracted_dir" ]; then
461-
mv "$jdk_extracted_dir"/* "$jdk_home"/ || die "failed to move JDK contents"
462-
else
463-
die "Could not find JDK directory in extracted zip"
464-
fi
456+
unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$jdk_file" -d "$jdk_extract_dir" || die "failed to extract JDK zip"
465457
else
466458
die "Cannot extract JDK zip: unzip command not available"
467459
fi
@@ -471,6 +463,28 @@ install_jdk() {
471463
;;
472464
esac
473465

466+
# Find the actual JDK root directory (where bin/java exists)
467+
verbose "Locating JDK root directory"
468+
local jdk_root_dir=""
469+
470+
# Search for bin/java in the extracted content (up to 3 levels deep to handle various archive structures)
471+
for candidate in "$jdk_extract_dir" "$jdk_extract_dir"/* "$jdk_extract_dir"/*/* "$jdk_extract_dir"/*/*/*; do
472+
if [ -f "$candidate/bin/java" ]; then
473+
jdk_root_dir="$candidate"
474+
verbose "Found JDK root at: $jdk_root_dir"
475+
break
476+
fi
477+
done
478+
479+
if [ -z "$jdk_root_dir" ]; then
480+
die "Could not locate JDK root directory with bin/java in extracted archive"
481+
fi
482+
483+
# Move the JDK root to the final location
484+
verbose "Installing JDK to: $jdk_home"
485+
mkdir -p "${jdk_home%/*}"
486+
mv "$jdk_root_dir" "$jdk_home" || die "failed to move JDK to final location"
487+
474488
# Verify JDK installation
475489
if [ ! -f "$jdk_home/bin/java" ]; then
476490
die "JDK installation failed: java executable not found at $jdk_home/bin/java"

0 commit comments

Comments
 (0)