Temporarily sets JAVA_HOME and PATH for the duration of the provided
code block, then restores the previous environment.
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").
- code
The code to execute with the temporary Java environment.
- ...
Additional arguments passed to
local_java_env().
Warning - Not for rJava
Do not use this function if your package depends on rJava.
See local_java_env() for details on why rJava is incompatible with scoped Java switching.
Examples
# \donttest{
# Using system2
rJavaEnv::with_java_env(version = 21, {
system2("java", "-version")
})
# Using processx
# rJavaEnv::with_java_env(version = 21, {
# processx::run("java", c("-jar", "tool.jar", "--help"))
# })
# }
