"""Java/JVM presence check with actionable error messages.""" from __future__ import annotations import shutil import subprocess import sys def ensure_java() -> None: """Raise a clear RuntimeError if Java is not installed.""" if shutil.which("java") is not None: return # Try jpype's own detection as a fallback try: import jpype # noqa: PLC0415 jpype.getDefaultJVMPath() return except Exception: # noqa: BLE001 pass _install_cmd = _get_install_cmd() raise RuntimeError( "\n" "╔══════════════════════════════════════════════════════════════╗\n" "║ TurkTokenizer requires Java (JVM) — not found on this system ║\n" "╠══════════════════════════════════════════════════════════════╣\n" f"║ Install Java with: ║\n" f"║ {_install_cmd:<58}║\n" "║ ║\n" "║ Then re-run your script. ║\n" "╚══════════════════════════════════════════════════════════════╝\n" ) def _get_install_cmd() -> str: if sys.platform == "linux": # Try to detect distro try: out = subprocess.check_output( ["cat", "/etc/os-release"], text=True, stderr=subprocess.DEVNULL ) if "ubuntu" in out.lower() or "debian" in out.lower(): return "sudo apt install default-jre" if "fedora" in out.lower() or "rhel" in out.lower() or "centos" in out.lower(): return "sudo dnf install java-latest-openjdk" if "arch" in out.lower(): return "sudo pacman -S jre-openjdk" except Exception: # noqa: BLE001 pass return "sudo apt install default-jre" if sys.platform == "darwin": return "brew install openjdk" if sys.platform == "win32": return "winget install Microsoft.OpenJDK.21" return "Install Java from https://adoptium.net"