Skip to content

Commit 4e95fa0

Browse files
committed
don't ask for consent in case of only session env change, add error for rJava already initialised
1 parent 9f9578d commit 4e95fa0

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

R/java_env.R

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,13 @@ java_env_set <- function(
3838
project_path = NULL,
3939
quiet = FALSE
4040
) {
41-
rje_consent_check()
42-
41+
4342
where <- match.arg(where)
4443
checkmate::assertString(java_home)
4544
checkmate::assertFlag(quiet)
46-
47-
48-
45+
46+
47+
4948
if (where %in% c("session", "both")) {
5049
java_env_set_session(java_home)
5150
if (!quiet) {
@@ -55,7 +54,8 @@ java_env_set <- function(
5554
))
5655
}
5756
}
58-
57+
58+
rje_consent_check()
5959
if (where %in% c("project", "both")) {
6060
# consistent with renv behavior for using
6161
# the current working directory by default
@@ -82,6 +82,14 @@ java_env_set <- function(
8282
#' @keywords internal
8383
#'
8484
java_env_set_session <- function(java_home) {
85+
86+
# check if rJava is installed and alread initialized
87+
if (requireNamespace("rJava", quietly = TRUE)) {
88+
if( rJava:::.jniInitialized == TRUE ) {
89+
cli::cli_inform(c("!" = "You have already initialised `rJava` directly or via your Java-dependent R package in the current session. `Java` version can only be set once per session for packages that rely on `rJava`. Unless you restart the R session or run your code in a new R subprocess using `targets` or `callr`, the new `JAVA_HOME` and `PATH` will not take effect."))
90+
}
91+
}
92+
8593
Sys.setenv(JAVA_HOME = java_home)
8694
old_path <- Sys.getenv("PATH")
8795
new_path <- file.path(java_home, "bin")

0 commit comments

Comments
 (0)