<a href="https://lifecycle.r-lib.org/articles/stages.html#experimental"
target="_blank"><img
src="https://img.shields.io/badge/lifecycle-experimental-orange.svg"
alt="Lifecycle: experimental" />
<a href="https://CRAN.R-project.org/package=rJavaEnv"
target="_blank"><img src="https://www.r-pkg.org/badges/version/rJavaEnv"
alt="CRAN status" />
The goal of rJavaEnv
is to manage multiple Java JDKs in R projects by
automating the process of downloading, installing, and configuring Java
environments on a per-project basis. This package is inspired by the
<a href="https://rstudio.github.io/renv/"
target="_blank">renv
package for managing R
environments in R projects.
You can request a specific Java Development Kit (JDK) in your project,
and rJavaEnv
will download and install the requested Java environment
in a project-specific directory and set the PATH and JAVA_HOME for when
you are using this project. Therefore, you can have different Java
versions for different projects without contaminating your system with
different Java versions.
WARNING This package is in the early stages of development and is not yet ready for production use. Please test it thoroughly before using it in your projects.
You can install the development version of rJavaEnv
from GitHub:
if (!requireNamespace("remotes", quietly = TRUE)) {
install.packages("remotes")
}
remotes::install_github("e-kotov/rJavaEnv")
rJavaEnv::java_quick_install(version = 21)
This will:
download Java 21 distribution compatible with the current operating system and processor architecture into a local cache folder;
extract the downloaded Java distribution into another cache folder;
create a symbolic link (for macOS and Linux) or junction (for Windows,
if that fails, just copies the files)
rjavaenv/platform
/processor_architecture
/java_version
in the
current directory/project to point to the cached installation;
set the current session’s JAVA_HOME and PATH environment variables to point to the installed (symlinked) Java distribution;
add code to .Rprofile file in the current directory/project to set JAVA_HOME and PATH environment variables when the project is opened in RStudio.
After that, you can even remove rJavaEnv
completely, as the Java
environment will be set up in the project directory with the base R code
that does not rely on rJavaEnv
.
The package has several core functions:
java_quick_install()
java_check_version_cmd()
opentripplanner
, that performs
Java calls using command line.java_version_check_rjava()
rJava
in a separate R
session. For rJava
-dependent packages such as
<a href="https://github.com/ipeaGIT/r5r"
target="_blank">r5r
.java_download()
java_install()
java_env_set()
java_env_unset()
java_list()
java_clear()
See more details on all the functions in the <a href="https://e-kotov.github.io/rJavaEnv/reference/index.html" target="_blank">Reference.
For detailed usage, see the Quick Start Vignette (work in progress).
Currently, rJavaEnv
only supports major Java versions such as 8, 11,
17, 21, 22. The download and install functions ignore the minor version
of the Java distribution and just downloads the latest stable subversion
of the specified major version. This is done to simplify the process and
avoid the need to update the package every time a new minor version of
Java is released. For most users this should be sufficient, but this is
substandard for full reproducibility.
The main limitation is that if you want to switch to another Java
environment, you will most likely have to restart the current R session
and set the JAVA_HOME and PATH environment variables to the desired Java
environment using rJavaEnv::java_env_set()
. This cannot be done
dynamically within the same R session due to the way Java is initialized
in R, particularly with the rJava
-dependent packages such as
<a href="https://github.com/ipeaGIT/r5r"
target="_blank">r5r
. With packages like
<a href="https://github.com/ropensci/opentripplanner"
target="_blank">opentripplanner
, that performs Java
calls using command line, you can switch Java environments dynamically
within the same R session as much as you want.
Therefore, if you need to use R packages that depend on different Java
versions within the same project, you will have to create separate R
scripts for each Java environment and run them in separate R sessions.
One effective way of doing this is to use the
<a href="https://github.com/r-lib/callr"
target="_blank">callr
package to run R scripts in
separate R sessions. Another option is to use the
<a href="https://github.com/ropensci/targets"
target="_blank">targets
package to manage the whole
project workflow, which, as a side effect, will lead to all R scripts
being run in separate R sessions. To use rJavaEnv
with targets
, you
will need to download and install several Java environments using
rJavaEnv::java_download()
and rJavaEnv::java_install()
and set the
relevant path with rJavaEnv::java_env_set()
at the beginning of each
function that requires a certain Java version.
The future work includes:
Add support for more Java distributions and versions
Take care of <a
href="https://solutions.posit.co/envs-pkgs/using-rjava/#reconfigure-r"
target="_blank">R CMD javareconf
Possibly add support for specifying Java version beyond the major version
Possibly allow downloading several Java distributions in one function call, e.g. different major versions of the same ‘flavour’ or different ‘flavours’ of the same major version
Possibly add automation to get the Java that is required by specific Java-dependent R packages
I am open to suggestions and contributions, welcome to <a href="https://github.com/e-kotov/rJavaEnv/issues" target="_blank">issues and pull requests.
I thank rOpenSci for the Dev Guide, as well as Hadley Wickham and Jennifer Bryan for the R Packages book.
Package hex sticker logo is partially generated by DALL-E by OpenAI. The logo also contains the original R logo.
To cite package ‘rJavaEnv’ in publications use:
Kotov E (2024). rJavaEnv: Java Environments for R Projects. <doi:10.5281/zenodo.11403010> https://doi.org/10.5281/zenodo.11403010, https://github.com/e-kotov/rJavaEnv.
BibTeX:
@Manual{rjavaenv,
title = {rJavaEnv: Java Environments for R Projects},
author = {Egor Kotov},
year = {2024},
url = {https://github.com/e-kotov/rJavaEnv},
doi = {10.5281/zenodo.11403010},
}