File size: 2,477 Bytes
ca41c16 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | """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"
|