Skip to content

Commit 54e904c

Browse files
committed
fix java_env_set_session
1 parent a083bde commit 54e904c

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

R/java_env.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,14 @@ java_env_set <- function(
8484
java_env_set_session <- function(java_home) {
8585

8686
# check if rJava is installed and alread initialized
87-
if (requireNamespace("rJava", quietly = TRUE)) {
88-
if( getFromNamespace(".jniInitialized", "rJava") == 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."))
87+
if (any(installed.packages()[, 1] == "rJava")) {
88+
if( "rJava" %in% loadedNamespaces() == TRUE ) {
89+
cli::cli_inform(c("!" = "You have `rJava` R package loaded in the current session. If you have already initialised it directly with ``rJava::.jinit()` or via your Java-dependent R package in the current session, you may not be able to switch to a different `Java` version unless you restart R. `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."))
9090
}
9191
}
9292

9393
Sys.setenv(JAVA_HOME = java_home)
94+
9495
old_path <- Sys.getenv("PATH")
9596
new_path <- file.path(java_home, "bin")
9697
Sys.setenv(PATH = paste(new_path, old_path, sep = .Platform$path.sep))

0 commit comments

Comments
 (0)