5

I have been around a problem with rJava in the newest version of R(3.4.1), Mac (10.12.5) and Java VM (1.8.0_131, x86_64:)

Problem:

I try it to follow this guide

scottdhoover.wordpress.com/2013/03/05/a-basic-rjava-example/

To be able to run rJava in R.

Some code from r:

library(rJava) .jinit("") .jclassPath() [1] "/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/java"

s <- .jnew("java/lang/String", "Hello World!") .jcall(s, "I", "length") [1] 12

So is actually working.. but when I try with my files:

.jaddClassPath("/Users/Camilo/Desktop/20170711_Java2R") .jclassPath() [1] "/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/java" "/Users/Camilo/Desktop/20170711_Java2R"

myExchange <- .jnew("myExchange") Error in .jnew("myExchange") : java.lang.ClassNotFoundException

where the file myExchange.java is the same that in the guide and is in the folder in the Desktop.

What I have done:

Basically I tried to instal rJava from R and didn't work. Then I have follow two guides to be able to load the package rJava in R from source

I have follow this guide:

https://github.com/snowflakedb/dplyr-snowflakedb/wiki/Configuring-R-rJava-RJDBC-on-Mac-OS-X (To be able to load rJava)

Where both

   R CMD javareconf
   /usr/libexec/java_home -V

Looks exactly as it should be.

To get rid of the OpenMP problem that people normally have I follow this guide:

http://thecoatlessprofessor.com/programming/openmp-in-r-on-os-x/#clang-before-3-4-0 (To get rid with the OpenMP problem that all people have).

and Finally I create a symlink using this code

  sudo ln -f -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib

and I was able to install the library from R.

But! When I try it from terminal I get this error

   ld: library not found for -lomp
   clang-4.0: 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’

So I tought: A library or a symlink must to be missing too.. but there is not such a file as in llvm ( Have been solve in llvm with ld: library not found for -lomp )

I know that the error "ClassNotFoundExceptions" could be related with the PATH but It should be OK cos has been declared at the beginning.

btw, I also check the .R/Makevars file that looks like this

  CC=/usr/local/clang4/bin/clang    
  CXX=/usr/local/clang4/bin/clang++
  LDFLAGS=-L/usr/local/clang4/lib

Any ideas how to solve it? Thanks in advance! I hope that is clear.

Chino Chiang
  • 91
  • 2
  • 6

2 Answers2

4

For me the solution was to install https://github.com/coatless/r-macos-clang and add link

sudo ln -s /usr/local/clang4/lib/libomp.dylib /usr/local/lib/libomp.dylib

Before R CMD javareconf i unset JAVA_HOME.

markush81
  • 201
  • 4
  • 10
  • I will try it during this week and let you know! thanks! ;) – Chino Chiang Sep 11 '17 at 10:50
  • I modified it for clang6 in High Sierra running R 3.5.1 in the obvious way and succeeded in compiling rJava 0.9-10 with the version 10 Java SDK – IRTFM May 29 '18 at 01:14
  • I should probably clarify that it was only the creation of the symlink that was needed to get rJava 0.9-10 to compile in the setting of a Makevars pointing to clang6. Did not go the route of making a different clang6 than the one offered at https://cloud.r-project.org/bin/macosx/tools/ Reporting this on the rJava issues page brought a response from Simon Urbanek that this was something that should already have been there in my Apple system, but apparently was not. – IRTFM Jun 03 '18 at 18:55
1

This is a confusing issue and depends on your R version.

For R 3.4 users on Mac Sierra, ignore all the noises about gcc, clang, llvm. R 3.4 uses clang4. Period. So you should do

  • Install clang4
  • Change .R/Makevars to point to clang4
  • Install Java 8
  • R CMD javareconf
  • To address a linking issue, sudo ln -s /usr/local/clang4/lib/libomp.dylib /usr/local/lib/libomp.dylib

Then you should be able to install rJava successfully.

In addition, rJava won't load properly in older RStudio. So make sure you've upgraded to the latest RStudio.

Steve Lihn
  • 357
  • 3
  • 5