Search code examples
javaramazon-web-servicesamazon-ec2ubuntu-server

Java, rJava install on AWS EC2 for R (xlsx and other packages)


I have reactivated my dormant Amazon EC2 microinstance with Ubuntu 16.04.LTS and RStudio (see sessionInfo() output below) running (thanks to AMI by Louis Aslett). After ssh-ing to the AWS instance first I've run sudo apt-get update.

When I tried installing some packages (e.g xlsx and others) in R Studio (I've got an error because of missing Java, which seems to be a common problem). I followed advices to 1) install dependencies (for e.g. xlsx like rJava, and xlsxjars) separately; 2) apt-get install r-cran-rjava as suggested here. Then checking the installed java -version showed

openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-8u131-b11-0ubuntu1.16.04.2-b11)
OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)

Similar problems were proposed "rJava load error in RStudio/R after “upgrading” to OSX Yosemite" and here "error: unable to load installed packages...", where the solution seemed to be linking the path of java to R export LD_LIBRARY_PATH=$JAVA_LD_LIBRARY_PATH or setting R's env. variables Sys.setenv(JAVA_HOME='/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server').

However, running R CMD javareconf -e revealed (I suppose) some deeper issues (see Image) &/or snippet:

R CMD javareconf -e
Java interpreter : /usr/lib/jvm/default-java/jre/bin/java
Java version     : 1.8.0_131
Java home path   : /usr/lib/jvm/default-java
Java compiler    : not present
Java headers gen.: 
Java archive tool: 

trying to compile and link a JNI program 
detected JNI cpp flags    : 
detected JNI linker flags : -L$(JAVA_HOME)/jre/lib/amd64/server -ljvm
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG      -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c conftest.c -o conftest.o
conftest.c:1:17: fatal error: jni.h: No such file or directory
compilation terminated.
/usr/lib/R/etc/Makeconf:132: recipe for target 'conftest.o' failed
make: *** [conftest.o] Error 1
Unable to compile a JNI program

The following Java variables have been exported:
JAVA_HOME JAVA JAVAC JAVAH JAR JAVA_LIBS JAVA_CPPFLAGS JAVA_LD_LIBRARY_PATH
Running: /bin/bash

with Java compiler: not present and

conftest.c:1:17: fatal error: jni.h: No such file or directory compilation terminated. /usr/lib/R/etc/Makeconf:132: recipe for target 'conftest.o' failed

Then I've found a question "how to make jni.h be found?": here the best solution suggested gcc -I/usr/lib/jvm/jdk1.7.0_07/include Checking the same directory cd /usr/lib/jvm/ and listing its content ls --all showed:

.default-java
.java-1.8.0-openjdk-amd64.jinfo
..java-1.8.0-openjdk-amd64 java-8-openjdk-amd64

Running, gcc -I/usr/lib/jvm/java-1.8.0-openjdk-amd64/include resulted in: gcc: fatal error: no input files Additionally, I have tried sudo apt-get install openjdk-X-jdk (it was among the suggested answers "how to make jni.h be found?") but probably the package does not exist anymore "Unable to locate package openjdk-X-jdk".

Unfortunately, I am stuck and not skilled enough to implement other (seemingly more complicated solutions). I would really appreciate some help/input.

R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.1 LTS

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] parallel  compiler  stats     graphics  grDevices utils     datasets 
[8] methods   base     

other attached packages:
 [1] openxlsx_4.0.17        qdapTools_1.3.1        qdapRegex_0.7.2       
 [4] qdapDictionaries_1.0.6 stringr_1.1.0          stringi_1.1.2         
 [7] dplyr_0.5.0            tm_0.7-1               NLP_0.1-10            
[10] scales_0.4.0           RColorBrewer_1.1-2     ParallelForest_1.1.0  
[13] doMC_1.3.4             iterators_1.0.8        foreach_1.4.3         
[16] boot_1.3-18            reshape2_1.4.1         glmm_1.1.1            
[19] Matrix_1.2-6           mvtnorm_1.0-5          trust_0.1-7           
[22] ggplot2_2.1.0          shiny_0.13.2          

loaded via a namespace (and not attached):
 [1] gtools_3.5.0      slam_0.1-40       lattice_0.20-33  
 [4] colorspace_1.2-7  htmltools_0.3.5   chron_2.3-50     
 [7] XML_3.98-1.7      DBI_0.5-1         plyr_1.8.4       
[10] munsell_0.4.3     gtable_0.2.0      codetools_0.2-14 
[13] httpuv_1.3.3      gender_0.5.1      Rcpp_0.12.7      
[16] xtable_1.8-2      gdata_2.17.0      mime_0.5         
[19] gridExtra_2.2.1   digest_0.6.10     grid_3.3.1       
[22] tools_3.3.1       bitops_1.0-6      magrittr_1.5     
[25] RCurl_1.95-4.8    tibble_1.2        data.table_1.10.4
[28] assertthat_0.1    R6_2.2.0          igraph_1.0.1     


Solution

  • You need to install JDK. Take a look here as well.

    http://www.owsiak.org/?p=3671

    where I describe how to play with R, Java and rJava package. Even though description is macOS based, it should work with Linux as well. It's quite general approach.

    Your issue is related to the fact you are using JRE instead of JDK. JNI headers are missing there.