Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: How to set up JDK to work with R (related: rJava, RStudio, xslx)

Tags:

I am new to both programming and R, however, I am trying to install the 'xlsx'-package (and also XLConnect) in order to manipulate Excel-files via R. I am running RStudio version 1.0.153, R version 3.4.2 on a Mac OSX 10.12.6.

First, I tried installing the package via RStudio with install.packages('xlsx') and the loading the package with library('xlsx'). I got this error:

> library('xlsx')
Loading required package: 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 '/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so': 
dlopen(/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
 Referenced from: /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so
 Reason: image not found
Error: package ‘rJava’ could not be loaded

So I tried installing the rJava package with install.packages('rJava') and load it with library('rJava').

>  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 '/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
  Referenced from: /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so
  Reason: image not found

Further, I tried to download and install rJava from CRAN directly via this link: https://cran.r-project.org/web/packages/rJava/index.html

I installed this in RStudio with the downloaded local file, and got this result:

> install.packages("~/Downloads/rJava_0.9-9 (1).tar.gz", repos = NULL, type = "source")
* installing *source* package ‘rJava’ ...
** package ‘rJava’ successfully unpacked and MD5 sums checked
checking for gcc... clang
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether clang accepts -g... yes
checking for clang option to accept ISO C89... none needed
checking how to run the C preprocessor... clang -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for ANSI C header files... 
rm: conftest.dSYM: is a directory
rm: conftest.dSYM: is a directory
yes
checking for sys/wait.h that is POSIX.1 compatible... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for string.h... (cached) yes
checking sys/time.h usability... yes
checking sys/time.h presence... yes
checking for sys/time.h... yes
checking for unistd.h... (cached) yes
checking for an ANSI C-conforming const... yes
checking whether time.h and sys/time.h may both be included... yes
configure: checking whether clang supports static inline...
yes
checking whether setjmp.h is POSIX.1 compatible... yes
checking whether sigsetjmp is declared... yes
checking whether siglongjmp is declared... yes
checking Java support in R... present:
interpreter : '/usr/bin/java'
archiver    : '/usr/bin/jar'
compiler    : '/usr/bin/javac'
header prep.: '/usr/bin/javah'
cpp flags   : '-I/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/../include/darwin'
java libs   : '-L/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/server -ljvm'
checking whether Java run-time works... yes
checking whether -Xrs is supported... yes
checking whether JNI programs can be compiled... 
configure: error: Cannot compile a simple JNI program. See config.log for details.

Make sure you have Java Development Kit installed and correctly registered in R.
If in doubt, re-run "R CMD javareconf" as root.

ERROR: configuration failed for package ‘rJava’
* removing ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava’
Warning in install.packages :
  installation of package ‘/Users/ivers1/Downloads/rJava_0.9-9 (1).tar.gz’ had non-zero exit status

I searched around to get solutions to this problem, and a couple of threads I found recommended running the following code from Terminal sudo r cmd javareconfig , which supposedly should reset the Java environment. However, when I run this code from terminal, I get the following result:

> sudo r cmd javareconf

ARGUMENT 'cmd' __ignored__

ARGUMENT 'javareconf' __ignored__


R version 3.4.2 (2017-09-28) -- "Short Summer"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)

... 
...
...

Terminal ignores the given arguments, and returns only version info of R.

Now I do not know what to do. Does anyone have suggestions on how to solve this?

like image 643
ivrs1 Avatar asked Nov 01 '17 14:11

ivrs1


1 Answers

I used the command that RStudio provides. Be careful with your capitalization, because the command is case-sensitive:

sudo R CMD javareconf
like image 118
Mike Walker Avatar answered Sep 22 '22 13:09

Mike Walker