Skip to contents

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_HOME to ~/.Renviron and set OS-level environment variables. Default: TRUE in interactive sessions.

setup_rjava

Logical. Whether to install/configure rJava. Default: TRUE.

verbose

Logical. Default: TRUE.

Value

Invisibly, the path to the JDK home directory.

Details

The function performs the following steps:

  1. Asks for consent (interactive mode only)

  2. Scans for existing Java installations on the system

  3. Offers to remove incompatible (non-21) Java versions

  4. Uses an existing Java 21 if found, or downloads Adoptium JDK 21

  5. Configures JAVA_HOME and PATH for the current R session

  6. Persists JAVA_HOME to ~/.Renviron

  7. Sets JAVA_HOME at the OS level (Windows User env / shell config)

  8. Adds JDK bin/ to the system PATH

  9. Optionally installs and configures rJava

  10. 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.

Examples

if (FALSE) { # \dontrun{
setup_java()
setup_java(setup_rjava = FALSE)
} # }