e-kotov / rJavaEnv

Java Environments for R Projects
http://www.ekotov.pro/rJavaEnv/
Other
7 stars 0 forks source link

need to install Java with rJavaEnv before installing rJava? #8

Closed e-kotov closed 4 days ago

e-kotov commented 2 weeks ago

Consider the case when, say, a stock Rocker RStudio image is used. It has no Java. Or another clean system that does not have any version of any flavour of Java installed. In this case {rJava} may install, but will fail to load. See below.

install.packages("rJava")
#> Installing package into ‘/usr/local/lib/R/site-library’
#> (as ‘lib’ is unspecified)
#> trying URL 'https://p3m.dev/cran/__linux__/jammy/2024-06-13/src/contrib/rJava_1.0-11.tar.gz'
#> Content type 'binary/octet-stream' length 940665 bytes (918 KB)
#> ==================================================
#> downloaded 918 KB
#> 
#> * installing *binary* package ‘rJava’ ...
#> * DONE (rJava)
#> 
#> The downloaded source packages are in
#>  ‘/tmp/RtmpNw93ye/downloaded_packages’
library(rJava)
#> Error: package or namespace load failed for ‘rJava’:
#>  .onLoad failed in loadNamespace() for 'rJava', details:
#>   call: dyn.load(file, DLLpath = DLLpath, ...)
#>   error: unable to load shared object '/usr/local/lib/R/site-library/rJava/libs/rJava.so':
#>   libjvm.so: cannot open shared object file: No such file or directory

Therefore, if we install Java with:

rJavaEnv::java_quick_install(21)

We will get positive results for tools that call Java via command line:

> rJavaEnv::java_check_version_cmd()
JAVA_HOME: /home/rstudio/.cache/R/rJavaEnv/installed/linux/x64/21
Java path: /home/rstudio/.cache/R/rJavaEnv/installed/linux/x64/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

But rJava-dependent packages will fail:

> rJavaEnv::java_check_version_rjava()
✖ rJava package is not installed. You need to install rJava to use this function to check if rJava-based packages will work with the specified Java version.
[1] FALSE

But {rJava} cannot be installed and loaded successfully even after rJavaEnv::java_quick_install(21).

So in that case, do we need to address the #3 with rJavaEnv?

e-kotov commented 2 weeks ago

Note: this is does NOT seem to be an issue if the system has some kind of Java already pre-installed using conventional methods. For example, there is no such problem in Rocker Geospatial image, as it already comes with Java 11.

e-kotov commented 2 weeks ago

when running in a Docker image (testing here with Binder https://github.com/e-kotov/r5r-containerized ) and also in macOS (see here https://github.com/ipeaGIT/r5r/issues/393 ), Java version is NOT recognized by setup_r5() from {r5r} or during the manual code execution of the internals of setup_r5(). For example:

rJavaEnv::java_env_set(here::here("rjavaenv/linux/x64/21/"))
✔ Current R Session: JAVA_HOME and PATH set to /home/rstudio/rjavaenv/linux/x64/21/
✔ Current R Project/Working Directory: JAVA_HOME and PATH set to '/home/rstudio/rjavaenv/linux/x64/21/' in .Rprofile in '/home/rstudio/.Rprofile'
> 
> rJavaEnv::java_check_version_rjava()
Using current session's JAVA_HOME: /home/rstudio/rjavaenv/linux/x64/21/
With the user-specified JAVA_HOME rJava and other rJava/Java-based packages will use Java version:
"21.0.3"
[1] TRUE
> Sys.getenv("JAVA_HOME")
[1] "/home/rstudio/rjavaenv/linux/x64/21/"
> rJava::.jinit()
> ver <- rJava::.jcall("java.lang.System", "S", "getProperty", 
+                      "java.version")
> ver
[1] "11.0.23"
e-kotov commented 2 weeks ago

Sort of solved, can be tested here https://github.com/e-kotov/r5r-containerized . The issue is that R CMD javareconf has to be executed with root privileges, which probably is a huge problem. Currently it is unclear which setups are affected and why. On regular macOS and Windows machines it does not seem to be an issue, but in Docker containers and one some macs (example problem here https://github.com/ipeaGIT/r5r/issues/393 ) - it is.

e-kotov commented 4 days ago

Performed tests on vanilla systems with no Java installed (#15) , and specifically on Windows and macOS with R packages installed from both source and binaries.

Based on these tests, I conclude that as long as user's setup allows to install {rJava} from binaries and does not need to be build from source, {rJavaEnv} performs well and does not require ANY previously installed Java on Windows or macOS. R CMD javareconf (see #3) is also NOT needed. If the user wants, for whatever reason, to install {rJava} from source, R CMD javareconf is required.

On Linux (although further tests are required), Java does not need to be installed to be able to install {rJava} from a repository that has pre-built binaries for Linux (such as Posit Package Manager), but even that will not help unless we instruct the user to do R CMD javareconf.

Conclusion

So, Java does need to be installed (conventionally, but can also be installed with {rJavaEnv}) before building {rJava} from source (which makes sense). Otherwise, if {rJava} is installed form pre-built binaries, no Java is required until the user tries to initialise Java from R using {rJava} or {rJava}-dependent package.