Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 44 additions & 12 deletions core/src/main/shell/jeka
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -851,18 +851,50 @@ get_or_download_jdk() {
local download_url="https://api.foojay.io/disco/v3.0/directuris?distro=$JDK_DOWNLOAD_DISTRIB&javafx_bundled=false&libc_type=$JDK_DOWNLOAD_LIBC_TYPE&archive_type=$JDK_DOWNLOAD_FILE_TYPE&operating_system=$JDK_DOWNLOAD_OS&package_type=jdk&version=$JAVA_VERSION&architecture=$JDK_DOWNLOAD_ARCH&latest=available"
info "Downloading JDK $JDK_DOWNLOAD_DISTRIB $JAVA_VERSION to $jdk_cache_dir. It may take a while..."
download_and_unpack "$download_url" "$jdk_cache_dir" "$JDK_DOWNLOAD_FILE_TYPE"
if [ "tar.gz" == "$JDK_DOWNLOAD_FILE_TYPE" ]; then
pushd "$jdk_cache_dir" > /dev/null 2>&1
local nested_dir
nested_dir=$(find "." -mindepth 1 -maxdepth 1 -type d | head -n 1 | cut -c 3-)
popd > /dev/null 2>&1
temp_dir=$(mktemp -d)
if [ "$JDK_DOWNLOAD_OS" = "mac" ]; then
nested_dir+="/Contents/Home"
fi
mv "$jdk_cache_dir"/"$nested_dir"/* "$temp_dir"
mv "$temp_dir"/* "$jdk_cache_dir"
fi
if [ "tar.gz" == "$JDK_DOWNLOAD_FILE_TYPE" ]; then
pushd "$jdk_cache_dir" > /dev/null 2>&1

# Find the bin directory to handle various JDK archive structures
# Some distributions (e.g., Azul 8) include extra directory levels
local bin_dir
if [ "$JDK_DOWNLOAD_OS" = "mac" ]; then
# macOS JDKs have a special structure: jdk-dir/Contents/Home/bin
bin_dir=$(find "." -type d -path "*/Contents/Home/bin" | head -n 1)
else
# For other OS, find bin directory at any depth (up to 3 levels)
bin_dir=$(find "." -maxdepth 6 -type f -path "*/bin/javac" | head -n 1)
bin_dir=$(dirname "$bin_dir")

fi

if [ -z "$bin_dir" ]; then
popd > /dev/null 2>&1
msg "Error: Could not locate bin directory in downloaded JDK"
exit 1
fi

# Get the JDK root directory (parent of bin)
local jdk_root
jdk_root=$(dirname "$bin_dir")

# Verify this is a valid JDK by checking for javac
if [ ! -f "$jdk_root/bin/javac" ]; then
popd > /dev/null 2>&1
msg "Error: Invalid JDK structure - javac not found in $jdk_root/bin"
exit 1
fi

popd > /dev/null 2>&1

# Move JDK contents to cache root if needed
if [ "$jdk_root" != "." ]; then
temp_dir=$(mktemp -d)
mv "$jdk_cache_dir"/"${jdk_root#./}"/* "$temp_dir"
rm -rf "$jdk_cache_dir"/*
mv "$temp_dir"/* "$jdk_cache_dir"
rm -rf "$temp_dir"
fi
fi
fi
DOWNLOAD_JDK_DIR=$jdk_cache_dir
}
Expand Down
102 changes: 102 additions & 0 deletions core/src/main/shell/test_jdk_fix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/bash

echo "================================"
echo "Testing JDK Structure Detection"
echo "================================"

# Simulate the fixed logic
test_structure() {
local test_name="$1"
local test_dir="$2"

echo -e "\n--- $test_name ---"

pushd "$test_dir" > /dev/null 2>&1

# This is your fix logic
local bin_dir
bin_dir=$(find "." -maxdepth 4 -type d -name "bin" | head -n 1)

if [ -z "$bin_dir" ]; then
echo "❌ FAIL: Could not find bin directory"
popd > /dev/null 2>&1
return 1
fi

local jdk_root
jdk_root=$(dirname "$bin_dir")

if [ ! -f "$jdk_root/bin/javac" ]; then
echo "❌ FAIL: javac not found in $jdk_root/bin"
popd > /dev/null 2>&1
return 1
fi

echo "✅ PASS: Found bin at $bin_dir"
echo " JDK root: $jdk_root"
echo " javac found: $jdk_root/bin/javac"

popd > /dev/null 2>&1
return 0
}

# Create test directories
TEST_BASE="/tmp/jeka_test_$$"
rm -rf "$TEST_BASE"
mkdir -p "$TEST_BASE"

echo "Creating test JDK structures..."

# Test 1: Standard structure (like Temurin)
echo "Setting up Test 1: Standard structure"
mkdir -p "$TEST_BASE/standard/jdk-21.0.1/bin"
touch "$TEST_BASE/standard/jdk-21.0.1/bin/javac"
touch "$TEST_BASE/standard/jdk-21.0.1/bin/java"
chmod +x "$TEST_BASE/standard/jdk-21.0.1/bin/"*

# Test 2: Azul 8 nested structure (THE PROBLEM CASE)
echo "Setting up Test 2: Azul 8 nested structure"
mkdir -p "$TEST_BASE/azul8/zulu8.78.0.19-ca-jdk8.0.412/zulu-8.jdk/bin"
touch "$TEST_BASE/azul8/zulu8.78.0.19-ca-jdk8.0.412/zulu-8.jdk/bin/javac"
touch "$TEST_BASE/azul8/zulu8.78.0.19-ca-jdk8.0.412/zulu-8.jdk/bin/java"
chmod +x "$TEST_BASE/azul8/zulu8.78.0.19-ca-jdk8.0.412/zulu-8.jdk/bin/"*

# Test 3: Triple nested (edge case)
echo "Setting up Test 3: Triple nested structure"
mkdir -p "$TEST_BASE/triple/level1/level2/level3/bin"
touch "$TEST_BASE/triple/level1/level2/level3/bin/javac"
touch "$TEST_BASE/triple/level1/level2/level3/bin/java"
chmod +x "$TEST_BASE/triple/level1/level2/level3/bin/"*

echo -e "\n================================"
echo "Running Tests"
echo "================================"

# Run tests
test_structure "Test 1: Standard JDK (Temurin-like)" "$TEST_BASE/standard"
TEST1=$?

test_structure "Test 2: Azul 8 Nested Structure (THE FIX)" "$TEST_BASE/azul8"
TEST2=$?

test_structure "Test 3: Triple Nested (Edge Case)" "$TEST_BASE/triple"
TEST3=$?

# Cleanup
rm -rf "$TEST_BASE"

# Summary
echo -e "\n================================"
echo "Test Summary"
echo "================================"
[ $TEST1 -eq 0 ] && echo "✅ Standard structure: PASS" || echo "❌ Standard structure: FAIL"
[ $TEST2 -eq 0 ] && echo "✅ Azul 8 structure: PASS" || echo "❌ Azul 8 structure: FAIL"
[ $TEST3 -eq 0 ] && echo "✅ Triple nested: PASS" || echo "❌ Triple nested: FAIL"

if [ $TEST1 -eq 0 ] && [ $TEST2 -eq 0 ] && [ $TEST3 -eq 0 ]; then
echo -e "\n🎉 All tests PASSED! Your fix works!"
exit 0
else
echo -e "\n❌ Some tests FAILED. Review the output above."
exit 1
fi