Skip to content

Commit

Permalink
fix java_env_set_session
Browse files Browse the repository at this point in the history
  • Loading branch information
e-kotov committed Sep 13, 2024
1 parent a083bde commit 54e904c
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions R/java_env.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ java_env_set <- function(
java_env_set_session <- function(java_home) {

# check if rJava is installed and alread initialized
if (requireNamespace("rJava", quietly = TRUE)) {
if( getFromNamespace(".jniInitialized", "rJava") == TRUE ) {
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."))
if (any(installed.packages()[, 1] == "rJava")) {
if( "rJava" %in% loadedNamespaces() == TRUE ) {
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."))
}
}

Sys.setenv(JAVA_HOME = java_home)

old_path <- Sys.getenv("PATH")
new_path <- file.path(java_home, "bin")
Sys.setenv(PATH = paste(new_path, old_path, sep = .Platform$path.sep))
Expand Down

0 comments on commit 54e904c

Please sign in to comment.