0

Trying to use rJava and have it use java version 11.

Currently it uses a previous installation I had of version 17.

When I run R CMD javareconf most of the settings look right.

Java interpreter : /Users/adam/.jenv/shims/java
Java version     : 11.0.13
Java home path   : /Library/Java/JavaVirtualMachines/zulu11.52.13-ca-jdk11.0.13-macosx_aarch64/zulu-11.jdk/Contents/Home
Java compiler    : /Users/adam/.jenv/shims/javac
Java headers gen.: /Users/adam/.jenv/shims/javah
Java archive tool: /Users/adam/.jenv/shims/jar
...

and yet when I call rJava::.jcall("java.lang.System","S","getProperty","java.version") I still get version 17.

Consequently I've tried re-installing rJava from source. When I do this the installation recognizes Java 11 as the "default" java installation to use, but I run into a problem with my cpp compiler, as I see the error

ld: library not found for -lpcre2-8
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [libjri.jnilib] Error 1
make[1]: *** [src/JRI.jar] Error 2
make: *** [jri] Error 2
ERROR: compilation failed for package ‘rJava’

when I run install.packages("rJava",type="source").

Other questions(1,2,3) have similar issues but my attempts to replicate the installation of the linker libraries, libpcre2-dev libbz2-dev zlib1g-dev as recommended to an ubuntu user did not work for me – I have not been able to find out how I might install the library as a Mac user . Furthermore, I'm not exactly sure if the "linkages" are setup appropriately in such cases.

Question How/Where can I find and install the -lpcre2-8 package for the mac version specified below and ensure that the appropriate linkages are in place for the rJava compilation?

Details I'm working on a Mac OS v12.0.1 (M1 chip), R v.4.1.2 using Rstudio v1.4.1717 to run R.

Adam
  • 55
  • 7

1 Answers1

0

First, re-compiling won't have any effect. The Java version used on macOS is governed by:

  • the JAVA_HOME environment variable
  • system Java selection (see /usr/libexec/java_home)

Normally, JAVA_HOME is not set, so your macOS system setting is used. You can see your installed Java versions with /usr/libexec/java_home -V including which is selected:

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    17, x86_64: "Java SE 17"    /Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
    1.8.0_252, x86_64:  "AdoptOpenJDK 8"    /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home

If you want to change the default, you can set JAVA_HOME to the preferred value. For example:

## use Java 1.8
$ JAVA_HOME=`/usr/libexec/java_home -v 1.8` Rscript -e \
  'print(rJava::J("java.lang.System")$getProperty("java.version"))'
[1] "1.8.0_252"

## use Java 17
$ JAVA_HOME=`/usr/libexec/java_home -v 17` Rscript -e \
  'print(rJava::J("java.lang.System")$getProperty("java.version"))'
[1] "17"

Second, if you want to re-compile rJava, you will need all libraries used to build R. For CRAN R build those are provided in https://mac.r-project.org/bin/, but I would not recommend going there unless you want to modify rJava. You can make your life easier by not compiling JRI (embedding R in Java) support, i.e. adding --disable-jri since it's that part which requires the libraries used by R. Just the rJava package itself doesn't need it.

Finally, it's usually a lot faster to ask on the support page rJava Discussion.

Simon Urbanek
  • 13,842
  • 45
  • 45