Skip to content

Commit 1b5ea2f

Browse files
authored
Merge pull request #70 from e-kotov/macos-corretto-versions-warning
platform dependent fallback java versions, automatic detection for current platform option
2 parents 227bdd3 + 4ffb69a commit 1b5ea2f

17 files changed

+162
-35
lines changed

CITATION.cff

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ authors:
3535
given-names: Egor
3636
3737
orcid: https://orcid.org/0000-0001-6690-5345
38+
- family-names: Chan
39+
given-names: Chung-hong
40+
41+
orcid: https://orcid.org/0000-0002-6232-7530
3842
preferred-citation:
3943
type: manual
4044
title: 'rJavaEnv: Java Environments for R Projects'

DESCRIPTION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Version: 0.2.2.9000
44
Authors@R: c(
55
person("Egor", "Kotov", , "[email protected]", role = c("aut", "cre", "cph"),
66
comment = c(ORCID = "0000-0001-6690-5345")),
7+
person("Chung-hong", "Chan", , "[email protected]", role = "aut",
8+
comment = c(ORCID = "0000-0002-6232-7530")),
79
person("Mauricio", "Vargas", , "[email protected]", role = "ctb",
810
comment = c(ORCID = "0000-0003-1017-7574")),
911
person("Hadley", "Wickham", , "[email protected]", role = "ctb",

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- `Java` version (currently still only for `Amazon Corretto`) is now determined dynamically using the official GitHub `json` with releases, so when new `Java` version becomes available, you will not be depenent on `rJavaEnv` to be updated. As a fallback, versions up to 24 are hardcoded.
66
- `force` argument in `java_download()`. When set to `TRUE`, allows to overwrite the distribution file if it already exist in the cache.
7+
- new function `java_valid_versions()` allows to retrieve a list of all available `Java` versions for the current automatically detected OS and CPU architecture, or user-specified platform and architecture.
78

89
## Improvements
910

R/internal_utilities.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ platform_detect <- function(quiet = TRUE) {
2020
"x86-64" = "x64",
2121
"x86_64" = "x64",
2222
"i386" = "x86",
23+
"i486" = "x86",
24+
"i586" = "x86",
2325
"i686" = "x86",
2426
"aarch64" = "aarch64",
2527
"arm64" = "aarch64",

R/java_download.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#' Download a Java distribution
22
#'
3-
#' @param version `Integer` or `character` vector of length 1 for major version of Java to download or install. If not specified, defaults to the latest LTS version. Can be "8", "11", "17", "21", "22", "23", "24", or 8, 11, 17, 21, 22, 23, 24 or any newer version if it is available for the selected distribution.
3+
#' @param version `Integer` or `character` vector of length 1 for major version of Java to download or install. If not specified, defaults to the latest LTS version. Can be "8", and "11" to "24" (or the same version numers in `integer`) or any newer version if it is available for the selected distribution. For `macOS` on `aarch64` architecture (Apple Silicion) certain `Java` versions are not available.
44
#' @param distribution The Java distribution to download. If not specified, defaults to "Amazon Corretto". Currently only \href{https://aws.amazon.com/corretto/}{"Amazon Corretto"} is supported.
55
#' @param cache_path The destination directory to download the Java distribution to. Defaults to a user-specific data directory.
66
#' @param platform The platform for which to download the Java distribution. Defaults to the current platform.
@@ -78,7 +78,11 @@ java_download <- function(
7878
version <- as.character(version)
7979
checkmate::assert_choice(
8080
as.character(version),
81-
java_valid_versions()
81+
java_valid_versions(
82+
distribution = distribution,
83+
platform = platform,
84+
arch = arch
85+
)
8286
)
8387

8488
checkmate::assert_choice(distribution, valid_distributions)

R/java_valid_versions.R

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#' The result is cached for 8 hours to avoid repeated API calls. If the API call fails (for example, due to a lack of internet connectivity),
55
#' the function falls back to a pre-defined list of Java versions.
66
#'
7-
#' @param distribution Character. The Java distribution to use. If set to `"Oracle"`, the Oracle API is used.
8-
#' If set to `"Corretto"` (the default), an Amazon Corretto endpoint is used.
7+
#' @inheritParams java_download
8+
#'
99
#' @param force Logical. If TRUE, forces a fresh API call even if a cached value exists. Defaults to FALSE.
1010
#'
1111
#' @return A character vector of valid Java versions.
@@ -23,6 +23,8 @@
2323
#'
2424
java_valid_versions <- function(
2525
distribution = "Corretto",
26+
platform = platform_detect()$os,
27+
arch = platform_detect()$arch,
2628
force = FALSE
2729
) {
2830
# Define cache expiry time (in hours)
@@ -51,7 +53,10 @@ java_valid_versions <- function(
5153
new_versions <- switch(
5254
distribution,
5355
# "Oracle" = java_valid_versions_oracle(),
54-
"Corretto" = java_valid_major_versions_corretto(),
56+
"Corretto" = java_valid_major_versions_corretto(
57+
platform = platform,
58+
arch = arch
59+
),
5560
stop("Unsupported distribution")
5661
)
5762

@@ -76,8 +81,7 @@ java_valid_versions <- function(
7681
#' If \code{NULL}, it is inferred using \code{platform_detect()}.
7782
#' @param platform Optional character string for the operating system (e.g., "windows", "macos", "linux").
7883
#' If \code{NULL}, it is inferred using \code{platform_detect()}.
79-
#' @param imageType Optional character string to filter on; defaults to \code{"jdk"}.
80-
#' @param extension Optional character string specifying the desired file extension; defaults to \code{"tar.gz"}.
84+
#' @param imageType Optional character string to filter on; defaults to \code{"jdk"}. Can be set to \code{"jre"} for Windows Java Runtime Environment.
8185
#'
8286
#' @return A `character` vector of available major Corretto versions.
8387
#'
@@ -86,8 +90,7 @@ java_valid_versions <- function(
8690
java_valid_major_versions_corretto <- function(
8791
arch = NULL,
8892
platform = NULL,
89-
imageType = "jdk",
90-
extension = "tar.gz"
93+
imageType = "jdk"
9194
) {
9295
# If platform or arch are not provided, detect them using the existing function.
9396
if (is.null(platform) || is.null(arch)) {
@@ -112,7 +115,8 @@ java_valid_major_versions_corretto <- function(
112115
names(eligible)
113116
},
114117
error = function(e) {
115-
getOption("rJavaEnv.fallback_valid_versions")
118+
platform_arch <- paste(platform, arch, sep = "_")
119+
getOption(paste0("rJavaEnv.fallback_valid_versions_", platform_arch))
116120
}
117121
)
118122

@@ -134,7 +138,7 @@ java_valid_major_versions_corretto <- function(
134138
# },
135139
# error = function(e) {
136140
# # If the API call fails, use the fallback list stored in options.
137-
# getOption("rJavaEnv.fallback_valid_versions")
141+
# getOption("rJavaEnv.fallback_valid_versions_current_platform")
138142
# }
139143
# )
140144
# }

R/onLoad.R

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
.onLoad <- function(libname, pkgname) {
2+
# First, set all the base rJavaEnv options
23
op <- options()
34
op.rJavaEnv <- list(
4-
# this default folder choice is in-line with what renv package does
5-
# https://github.com/rstudio/renv/blob/d6bced36afa0ad56719ca78be6773e9b4bbb078f/R/bootstrap.R#L940-L950
5+
# Default folder choice (in line with renv package)
66
rJavaEnv.cache_path = tools::R_user_dir("rJavaEnv", which = "cache"),
77
rJavaEnv.valid_versions_cache = NULL,
88
rJavaEnv.valid_versions_timestamp = NULL,
9-
# Fallback list of valid Java versions if the API call fails (e.g., no internet) as of 2025-04-02
10-
rJavaEnv.fallback_valid_versions = c(
9+
# Fallback lists for various platforms
10+
rJavaEnv.fallback_valid_versions_macos_aarch64 = c(
1111
"8",
1212
"11",
13-
# "15", # not available from Amazon Corretto
14-
# "16", # not available from Amazon Corretto
1513
"17",
1614
"18",
1715
"19",
@@ -20,10 +18,90 @@
2018
"22",
2119
"23",
2220
"24"
21+
),
22+
rJavaEnv.fallback_valid_versions_macos_x64 = c(
23+
"8",
24+
"11",
25+
"15",
26+
"16",
27+
"17",
28+
"18",
29+
"19",
30+
"20",
31+
"21",
32+
"22",
33+
"23",
34+
"24"
35+
),
36+
rJavaEnv.fallback_valid_versions_linux_aarch64 = c(
37+
"8",
38+
"11",
39+
"15",
40+
"16",
41+
"17",
42+
"18",
43+
"19",
44+
"20",
45+
"21",
46+
"22",
47+
"23",
48+
"24"
49+
),
50+
rJavaEnv.fallback_valid_versions_linux_x64 = c(
51+
"8",
52+
"11",
53+
"15",
54+
"16",
55+
"17",
56+
"18",
57+
"19",
58+
"20",
59+
"21",
60+
"22",
61+
"23",
62+
"24"
63+
),
64+
rJavaEnv.fallback_valid_versions_windows_x64 = c(
65+
"8",
66+
"11",
67+
"15",
68+
"16",
69+
"17",
70+
"18",
71+
"19",
72+
"20",
73+
"21",
74+
"22",
75+
"23",
76+
"24"
77+
),
78+
rJavaEnv.fallback_valid_versions_windows_x86 = c(
79+
"8",
80+
"11"
2381
)
2482
)
83+
84+
# Only set the options that haven't been defined yet
2585
toset <- !(names(op.rJavaEnv) %in% names(op))
2686
if (any(toset)) options(op.rJavaEnv[toset])
2787

88+
# Now, detect the current platform (OS and architecture)
89+
platform <- platform_detect(quiet = TRUE)
90+
91+
# Build the option name dynamically based on platform$os and platform$arch.
92+
# For example, for macOS on x64, this results in "rJavaEnv.fallback_valid_versions_macos_x64"
93+
fallback_option_name <- paste0(
94+
"rJavaEnv.fallback_valid_versions_",
95+
platform$os,
96+
"_",
97+
platform$arch
98+
)
99+
100+
# Retrieve the corresponding fallback list using getOption()
101+
fallback_current <- getOption(fallback_option_name)
102+
103+
# Set the current platform valid versions option
104+
options(rJavaEnv.fallback_valid_versions_current_platform = fallback_current)
105+
28106
invisible()
29107
}

README.qmd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ The package has several core functions:
167167
9. `java_clear()`
168168
* Removes all or some `Java` versions linked in the current project (or cached distributions or installations).
169169

170-
10. `use_java()`
170+
10 `java_valid_versions()`
171+
* Lists all valid major `Java` versions that can be downloaded and installed for either current automatically detected OS and CPU architecture or user-specified platform and architecture.
172+
173+
11. `use_java()`
171174
* Same as `java_quick_install()`, but in a less intrusive way. Does not copy or link the `Java` installation folder from cache into the project directory and does not create or edit your `.Rprofile` file. Only sets requested java in the current R session.
172175

173176
See more details on all the functions in the [Reference](https://www.ekotov.pro/rJavaEnv/reference/index.html){target="_blank"}.

codemeta.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
"familyName": "Kotov",
2929
"email": "[email protected]",
3030
"@id": "https://orcid.org/0000-0001-6690-5345"
31+
},
32+
{
33+
"@type": "Person",
34+
"givenName": "Chung-hong",
35+
"familyName": "Chan",
36+
"email": "[email protected]",
37+
"@id": "https://orcid.org/0000-0002-6232-7530"
3138
}
3239
],
3340
"contributor": [
@@ -178,7 +185,7 @@
178185
},
179186
"SystemRequirements": null
180187
},
181-
"fileSize": "351771.806KB",
188+
"fileSize": "351785.837KB",
182189
"citation": [
183190
{
184191
"@type": "SoftwareSourceCode",

inst/schemaorg.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33
"@graph": [
44
{
55
"type": "SoftwareSourceCode",
6-
"author": {
7-
"id": "https://orcid.org/0000-0001-6690-5345"
8-
},
6+
"author": [
7+
{
8+
"id": "https://orcid.org/0000-0002-6232-7530",
9+
"type": "Person",
10+
"email": "[email protected]",
11+
"familyName": "Chan",
12+
"givenName": "Chung-hong"
13+
},
14+
{
15+
"id": "https://orcid.org/0000-0001-6690-5345"
16+
}
17+
],
918
"codeRepository": "https://github.com/e-kotov/rJavaEnv",
1019
"contributor": [
1120
{

0 commit comments

Comments
 (0)