Finds or installs Java and returns the path to JAVA_HOME. This function does not modify any environment variables.
Usage
java_resolve(
version = NULL,
type = c("exact", "min"),
distribution = "Corretto",
backend = getOption("rJavaEnv.backend", "native"),
install = TRUE,
accept_system_java = TRUE,
quiet = FALSE,
cache_path = getOption("rJavaEnv.cache_path"),
platform = platform_detect()$os,
arch = platform_detect()$arch,
.use_cache = FALSE
)Arguments
- version
Java version specification. Accepts:
Major version (e.g.,
21,17): Downloads the latest release for that major version.Specific version (e.g.,
"21.0.9","11.0.29"): Downloads the exact version.SDKMAN identifier (e.g.,
"25.0.1-amzn","24.0.2-open"): Uses the SDKMAN backend automatically. When an identifier is detected, thedistributionandbackendarguments are ignored and derived from the identifier. Find available identifiers in theidentifiercolumn ofjava_list_available(backend = "sdkman").
- type
Character.
"exact"(default) checks for exact version match."min"checks for version >=version.- distribution
Character. The Java distribution to download. Defaults to "Corretto". Ignored if
versionis a SDKMAN identifier.- backend
Download backend to use. One of "native" (vendor APIs) or "sdkman". Defaults to "native". Can also be set globally via
options(rJavaEnv.backend = "sdkman").- install
Logical. If
TRUE(default), attempts to download/install if missing. IfFALSE, returnsFALSEif the version is not found.- accept_system_java
Logical. If
TRUE(default), the function will scan the system for existing Java installations (usingJAVA_HOME,PATH, and OS-specific locations). If a system Java matching theversionandtyperequirements is found, it will be used. Set toFALSEto ignore system installations and strictly use anrJavaEnvmanaged version.- quiet
A
logicalvalue indicating whether to suppress messages. Can beTRUEorFALSE.- cache_path
The destination directory to download the Java distribution to. Defaults to a user-specific data directory.
- platform
The platform for which to download the Java distribution. Defaults to the current platform.
- arch
The architecture for which to download the Java distribution. Defaults to the current architecture.
- .use_cache
A
logicalvalue controlling caching behavior. IfFALSE(default), performs a fresh check each time (safe, reflects current state). IfTRUE, uses session-scoped cached results for performance in loops or repeated calls.Caching Behavior:
Session-scoped: Cache is cleared when R restarts
Key-based for version checks: Changes to JAVA_HOME create new cache entries
System-wide for scanning: Always recalculates current default Java
Performance Benefits:
First call: ~37-209ms (depending on operation)
Cached calls: <1ms
Prevents 30-100ms delays on every call in performance-critical code
When to Enable:
Package initialization code (
.onLoador similar)Loops calling the same function multiple times
Performance-critical paths with frequent version checks
When to Keep Default (FALSE):
Interactive use (one-off checks)
When you need current data reflecting recent Java installations
General-purpose function calls that aren't time-critical
