Run R Package that requires Java in MAC M2 Sonoma

I have an issue with a R package built on Java. I have a Mac M2 Sonoma. The problem may be related to the java version I have and its directory but I'm unable to fix it. Code and error below:

library(rJava)
Sys.setenv(JAVA_HOME="path/to/java")
J("java.lang.System")$getProperty("java.version")
[1] "23.0.2"

require(devtools)
install_github(repo = "amattioc/SDMX", subdir = "RJSDMX")
library(RJSDMX)
getProviders()

getProviders()
Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, :
java.lang.IllegalAccessException: class RJavaTools cannot access a member of class java.util.TreeMap$Entry (in module java.base) with modifiers "public"

The issue is due to Java's module system restrictions in newer versions. Try switching to an older LTS version like Java 17 or 11:

  1. Install Java 17 (if not installed):
    brew install --cask temurin17  
    
  2. Set Java 17 as default for R:
    Sys.setenv(JAVA_HOME="/Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home")  
    
  3. Restart R and try running the package again.

If the issue persists, check if rJava is correctly linked:

library(rJava)  
.jinit()  

If errors occur, reinstall rJava after setting JAVA_HOME.

Thanks.

We tried "Sys.setenv" restarting R but it did not work.

It was solved by a IT colleague inserting JAVA HOME -> OpenJDK 11 in the .Renviron file (we used version 11)

Così ho inserito JAVA_HOME -> OpenJDK 11 nel file .Renviron e questo ha funzionato