ipeaGIT/r5r

convinient function to check Java version

Closed this issue ยท 13 comments

Brief description of the problem:

In the documentation of r5r (vignettes, README, etc), we inform users they check what version of Java they have installed on their computer by running this on R console:

rJava::.jinit()
rJava::.jcall("java.lang.System", "S", "getProperty", "java.version")

This is not a big deal, but we could provide a simple and convinient function to do the same. This would look like this:

check_java_version <- function(){
  rJava::.jinit()
  rJava::.jcall("java.lang.System", "S", "getProperty", "java.version")
}

r5r::check_java_version()
#> "21.0.1"

Super simple really. I'm not sure the function name should be check_java_version() or simply java_version(). What do you all think?

There doesn't seem to be a java_version function in any of the existing CRAN packages. The name java_version sounds more like a property or state rather than a function. However, the check_java_version function is available in the pathfindR package. It is highly unlikely that an r5r user will encounter this conflict. Therefore, I suggest using check_java_version.

Thanks for sharing your thoughts, @e-kotov !

@rafapereirabr Would it not be even better to have a function that installs Java for the user? It is often a big hassle to go and install a specific version of Java and manage several Java versions side by side on the same system. I would suggest choosing a Java distribution (e.g., Corretto from Amazon, Azul, or Temurin; many options are overviewed here), getting the zipped/gzipped distribution, unpacking it locally in the project directory, and setting the environment inside R to point to JAVA_HOME before initializing rJava or another Java-dependent package such as r5r.

Previously, when I worked with r5r, opentripplanner, or graphhopper, I used to do manually all the steps above. Now I am working on a small R package to do that for me. Apart from several helper functions, the idea is to create a single simple function that just gets the required Java version working for the user within the current project with reasonable defaults. Would you be interested to test an alpha version of it and perhaps integrate it with r5r in the future?

Interesting @e-kotov . I use r5r and OTP a lot in GitHub actions and turn to https://github.com/actions/setup-java .

@stupidpupil thank you for the link! This seems super sophisticated and a lot to learn from.

@rafapereirabr You can try the work-in-progress version (though it is pretty stable at this point) here: https://github.com/e-kotov/rJavaEnv.

devtools::install_github("e-kotov/rJavaEnv")
rJavaEnv::java_quick_install(version = 21)
#> Platform detected or provided: macos
#> Architecture detected or provided: arm64
#> Detected platform: macos
#> Detected architecture: arm64
#> You can change the platform and architecture by specifying the `platform` and
#> `arch` arguments.
#> Downloading Java 21 (Corretto) for macos arm64 to
#> /Users/user1/Library/Caches/org.R-project.R/R/rJavaEnv/distrib/amazon-corretto-21-aarch64-macos-jdk.tar.gz
#> File already exists. Skipping download.
#> Java distribution amazon-corretto-21-aarch64-macos-jdk.tar.gz already unpacked
#> at
#> '/Users/user1/Library/Caches/org.R-project.R/R/rJavaEnv/installed/macos/aarch64/21'
#> โœ” Current R Session: JAVA_HOME and PATH set to '/Users/user1/Library/Caches/org.R-project.R/R/rJavaEnv/installed/macos/aarch64/21'
#>
#> โœ” Current R Project/Working Directory: JAVA_HOME and PATH set to ''/Users/user1/Library/Caches/org.R-project.R/R/rJavaEnv/installed/macos/aarch64/21'' in .Rprofile in ''/private/var/folders/gb/t5zr5rn15sldqybrmqbyh6y80000gn/T/Rtmp7RT2n5/reprex-111d8418bd000-tangy-heron/.Rprofile''
#>
#> Java 21 (amazon-corretto-21-aarch64-macos-jdk.tar.gz) for macos aarch64
#> installed at
#> '/Users/user1/Library/Caches/org.R-project.R/R/rJavaEnv/installed/macos/aarch64/21'
#> and symlinked to
#> '/private/var/folders/gb/t5zr5rn15sldqybrmqbyh6y80000gn/T/Rtmp7RT2n5/reprex-111d8418bd000-tangy-heron/rjavaenv/macos/aarch64/21'
rJavaEnv::java_check_version_cmd()
#> JAVA_HOME:
#> '/Users/user1/Library/Caches/org.R-project.R/R/rJavaEnv/installed/macos/aarch64/21'
#> Java path:
#> '/Users/user1/Library/Caches/org.R-project.R/R/rJavaEnv/installed/macos/aarch64/21/bin/java'
#> Java version: "openjdk version \"21.0.3\" 2024-04-16 LTS OpenJDK Runtime
#> Environment Corretto-21.0.3.9.1 (build 21.0.3+9-LTS) OpenJDK 64-Bit Server VM
#> Corretto-21.0.3.9.1 (build 21.0.3+9-LTS, mixed mode, sharing)"
#> [1] TRUE
rJavaEnv::java_check_version_rjava()
#> Using current session's JAVA_HOME:
#> '/Users/user1/Library/Caches/org.R-project.R/R/rJavaEnv/installed/macos/aarch64/21'
#> With the user-specified JAVA_HOME rJava and other rJava/Java-based packages
#> will use Java version: "21.0.3"
#> [1] TRUE

Created on 2024-05-31 with reprex v2.1.0

One thing to note is that, based on my testing, checking the Java version in the way you suggest is not convenient for the user. Once you initialize rJava in an R session, any actions to change the Java paths will not take effect until you restart the R session. My package provides a way to test the Java version both through the command line and through an rJava initialization call in a separate R session. More importantly, it allows users to conveniently install the relevant Java version. This installation sticks to their project, ensuring consistency. Currently only Amazon Corretto is supported and only major Java versions, the latest ones at the time of download. But I plan to add more distributions later, as it is just a matter of adding to the json file or building a few more functions to get the links from the Java distribution providers. For now, I am focusing on ensuring the main functionality of the package works well.

@rafapereirabr I am interested in your opinion, as I think your users are the primary audience of this package, as it is one of the top downloaded rJava dependent packages apart from xlsx, which is indifferent to the Java version as far as I know:

plot_zoom_png-19

Hi @e-kotov . I haven't tested {rJavaEnv} yet, but it looks super convinient and a much better solution than what I had initially in mind ! I'll be travelling over the next few weeks but I'll try to take it for a spin when I have time. Once {rJavaEnv} is on CRAN, I would be happy to suggest it to {r5r} users in our package documentation, vignette and our next book (currently in progress).

@rafapereirabr great! There's no rush. I still have tests to write and I want to submit to rOpenSci for review and feedback, and then to CRAN. So the later you will try it, the better it should be)

@rafapereirabr done, it's on the AppStore CRAN!

install.packages("rJavaEnv")

Due to CRAN policies, on first run I ask the user for consent. This can be skipped with additional command before the Java install command:

rje_consent(provided = TRUE)
java_quick_install(version = 21)

You may want to update your tutorial at: https://github.com/ipeaGIT/access_workshop_bogota_2024/blob/3ab95936abee362ef8b3aa2faa99c9076704a234/2.1_installation.qmd#L56-L70

Thanks for the heads up @e-kotov ! I just updated the website of the workshop! I guess we can close this issue ?

I guess we can close this issue ?

Sure!

I've updated the README and intro vignette now.