One-call setup that detects existing Java installations, downloads JDK 21 if needed, configures environment variables at both the R session and OS level, optionally installs and configures rJava, and verifies the setup.
Usage
setup_java(
install_dir = file.path(tools::R_user_dir("interpElections", "data"), "java"),
persist = interactive(),
setup_rjava = TRUE,
verbose = TRUE,
.ask_consent = TRUE
)Arguments
- install_dir
Character. Where to install Java if downloading. Default uses
tools::R_user_dir().- persist
Logical. Write
JAVA_HOMEto~/.Renvironand set OS-level environment variables. Default: TRUE in interactive sessions.- setup_rjava
Logical. Whether to install/configure rJava. Default: TRUE.
- verbose
Logical. Default: TRUE.
Details
The function performs the following steps:
Asks for consent (interactive mode only)
Scans for existing Java installations on the system
Offers to remove incompatible (non-21) Java versions
Uses an existing Java 21 if found, or downloads Adoptium JDK 21
Configures
JAVA_HOMEandPATHfor the current R sessionPersists
JAVA_HOMEto~/.RenvironSets
JAVA_HOMEat the OS level (Windows User env / shell config)Adds JDK
bin/to the system PATHOptionally installs and configures rJava
Runs a final verification
Even if Java 21 is already installed, calling setup_java() ensures
all configuration (env vars, PATH, rJava) is correctly wired up.
The function is idempotent: you can always call it to fix a broken
configuration without re-downloading.
See also
check_r5r() to diagnose without changing anything,
set_java_memory() to configure JVM heap size.