Skip to content

Commit 60fee15

Browse files
authored
Merge pull request #64 from JsLth/main
Make java version detection using `java -version` more robust
2 parents ff170a5 + 5c3f94d commit 60fee15

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

DESCRIPTION

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ Authors@R: c(
99
person("Hadley", "Wickham", , "[email protected]", role = "ctb",
1010
comment = "use_java feature suggestion and PR review"),
1111
person("Enrique", "Mondragon-Estrada", , "[email protected]", role = "ctb",
12-
comment = c(ORCID = "0009-0004-5592-1728"))
12+
comment = c(ORCID = "0009-0004-5592-1728")),
13+
person("Jonas", "Lieth", , "[email protected]", role = c("aut", "cre", "cph"),
14+
comment = c(ORCID = "0000-0002-3451-3176"))
1315
)
1416
Description: Quickly install 'Java Development Kit (JDK)' without
1517
administrative privileges and set environment variables in current R

NEWS.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
- `force` argument in `java_download()`. When set to `TRUE`, allows to overwrite the distribution file if it already exist in the cache.
66

7+
## Improvements
8+
9+
- better command line `Java` detection (thanks to Jonas Lieth)
10+
711
# rJavaEnv 0.2.2 (2024-09-13)
812

9-
* Hot fix: improve robustness of setting Java environment in the current session with either `use_java()` or `java_quick_install()`. See bug fix below.
13+
* Hot fix: improve robustness of setting `Java` environment in the current session with either `use_java()` or `java_quick_install()`. See bug fix below.
1014

1115
* Bug fix: Setting Java environment via `rJava::.jniInitialized()` rendered impossible changing Java version for `rJava`-dependent packages, because it somehow pre-initialised `rJava`
1216

R/java_env.R

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,34 @@
1717
#' project_path = tempdir(),
1818
#' autoset_java_env = FALSE
1919
#' )
20-
#'
20+
#'
2121
#' # now manually set the JAVA_HOME and PATH environment variables in current session
2222
#' java_env_set(
2323
#' where = "session",
2424
#' java_home = java_home
2525
#' )
26-
#'
26+
#'
2727
#' # or set JAVA_HOME and PATH in the spefific projects' .Rprofile
2828
#' java_env_set(
2929
#' where = "session",
3030
#' java_home = java_home,
3131
#' project_path = tempdir()
3232
#' )
33-
#'
33+
#'
3434
#' }
3535
java_env_set <- function(
3636
where = c("session", "both", "project"),
3737
java_home,
3838
project_path = NULL,
3939
quiet = FALSE
4040
) {
41-
41+
4242
where <- match.arg(where)
4343
checkmate::assertString(java_home)
4444
checkmate::assertFlag(quiet)
45-
46-
47-
45+
46+
47+
4848
if (where %in% c("session", "both")) {
4949
java_env_set_session(java_home)
5050
if (!quiet) {
@@ -54,16 +54,16 @@ java_env_set <- function(
5454
))
5555
}
5656
}
57-
57+
5858
rje_consent_check()
5959
if (where %in% c("project", "both")) {
6060
# consistent with renv behavior for using
6161
# the current working directory by default
6262
# https://github.com/rstudio/renv/blob/d6bced36afa0ad56719ca78be6773e9b4bbb078f/R/init.R#L69-L86
6363
project_path <- ifelse(is.null(project_path), getwd(), project_path)
64-
64+
6565
java_env_set_rprofile(java_home, project_path = project_path)
66-
66+
6767
if (!quiet) {
6868
cli::cli_alert_success(c(
6969
"Current R Project/Working Directory: ",
@@ -83,14 +83,14 @@ java_env_set <- function(
8383
#' @importFrom utils installed.packages
8484
#'
8585
java_env_set_session <- function(java_home) {
86-
86+
8787
# check if rJava is installed and alread initialized
8888
if (any(utils::installed.packages()[, 1] == "rJava")) {
8989
if( "rJava" %in% loadedNamespaces() == TRUE ) {
9090
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."))
9191
}
9292
}
93-
93+
9494
Sys.setenv(JAVA_HOME = java_home)
9595

9696
old_path <- Sys.getenv("PATH")
@@ -301,7 +301,7 @@ java_check_version_cmd <- function(
301301
#' @inheritParams java_check_version_cmd
302302
#' @return A `character` vector of length 1 containing the major Java version.
303303
#' @keywords internal
304-
#'
304+
#'
305305
java_check_version_system <- function(
306306
quiet
307307
) {
@@ -331,9 +331,9 @@ java_check_version_system <- function(
331331

332332
# extract Java version
333333
java_ver_string <- java_ver[[1]]
334-
matches <- gregexpr('(?<=openjdk version \\\")[0-9]{1,2}(?=\\.)', java_ver_string, perl = TRUE)
335-
major_java_ver <- regmatches(java_ver_string, matches)[[1]]
336-
334+
matches <- regexec('(openjdk|java) (version )?(\\\")?([0-9]{1,2})', java_ver_string)
335+
major_java_ver <- regmatches(java_ver_string, matches)[[1]][5]
336+
337337
# fix 1 to 8, as Java 8 prints "1.8"
338338
if (major_java_ver == "1") {
339339
major_java_ver <- "8"
@@ -361,7 +361,7 @@ java_env_unset <- function(
361361
quiet = FALSE
362362
) {
363363
rje_consent_check()
364-
364+
365365
# Resolve the project path
366366
# consistent with renv behavior
367367
# https://github.com/rstudio/renv/blob/d6bced36afa0ad56719ca78be6773e9b4bbb078f/R/init.R#L69-L86

0 commit comments

Comments
 (0)