#!/bin/bash # This script uninstalls any existing Java installation and then installs Java 11, # which is compatible with Gradle 6.7. # It supports Linux (apt, yum, or dnf) and macOS (using Homebrew). set -e # Exit immediately if a command fails # Uninstall Java on Linux uninstall_java_linux() { if command -v apt-get >/dev/null 2>&1; then echo "Uninstalling Java on apt-based system..." sudo apt-get remove --purge -y default-jre openjdk-* || true sudo apt-get autoremove -y || true elif command -v yum >/dev/null 2>&1; then echo "Uninstalling Java on yum-based system..." sudo yum remove -y java-* || true elif command -v dnf >/dev/null 2>&1; then echo "Uninstalling Java on dnf-based system..." sudo dnf remove -y java-* || true else echo "No supported package manager found. Please uninstall Java manually." exit 1 fi } # Uninstall Java on macOS uninstall_java_macos() { if ! command -v brew >/dev/null 2>&1; then echo "Homebrew is not installed. Please uninstall Java manually." exit 1 fi echo "Uninstalling Java using Homebrew..." brew uninstall --cask temurin@11 || echo "temurin@11 not installed via Homebrew; skipping." } # Install Java 11 on Linux install_java_11_linux() { if command -v apt-get >/dev/null 2>&1; then echo "Installing OpenJDK 11 on apt-based system..." sudo apt-get update sudo apt-get install -y openjdk-11-jdk elif command -v yum >/dev/null 2>&1; then echo "Installing OpenJDK 11 on yum-based system..." sudo yum install -y java-11-openjdk elif command -v dnf >/dev/null 2>&1; then echo "Installing OpenJDK 11 on dnf-based system..." sudo dnf install -y java-11-openjdk else echo "No supported package manager found. Please install Java 11 manually." exit 1 fi } # Install Java 11 on macOS using Homebrew install_java_11_macos() { if ! command -v brew >/dev/null 2>&1; then echo "Homebrew is not installed. Please install Java 11 manually." exit 1 fi echo "Installing OpenJDK 11 using Homebrew..." brew install --cask temurin@11 } # Check if Java 11 is installed by verifying its version output check_java_valid() { if java -version 2>&1 | grep -q '11'; then return 0 else return 1 fi } # Main script execution os_type=$(uname) echo "Detected OS: $os_type" if [ "$os_type" = "Darwin" ]; then uninstall_java_macos install_java_11_macos # Set JAVA_HOME and update PATH for macOS if [ -x "/usr/libexec/java_home" ]; then export JAVA_HOME=$(/usr/libexec/java_home -v 11) export PATH="$JAVA_HOME/bin:$PATH" echo "JAVA_HOME set to $JAVA_HOME" fi elif [ "$os_type" = "Linux" ]; then uninstall_java_linux install_java_11_linux # On Linux, you might need to update alternatives if multiple Java versions are installed. else echo "Unsupported OS: $os_type. Please install Java 11 manually." exit 1 fi echo "Verifying Java installation..." if check_java_valid; then echo "Java 11 installed successfully." else echo "Java 11 installation failed. Please check the installation and package names." exit 1 fi # Execute the run.sh script if it exists and is executable if [ -x "./run.sh" ]; then echo "Executing ./run.sh..." ./run.sh else echo "./run.sh not found or is not executable." exit 1 fi