25

I've tried to solve this using the previous questions/answers on SO but without any success. So, here's my problem.

I'm using RStudio on and Ubuntu box (14.04) and I tried to upgrade rJava from sources and in the process I managed to lose it.

I tried to install it again using,

install.packages("rJava")

which returned the following error message,

configure: error: One or more Java configuration variables are not set.
Make sure R is configured with full Java support (including JDK). Run
R CMD javareconf
as root to add Java support to R.

If you don't have root privileges, run
R CMD javareconf -e
to set all Java-related variables and then install rJava.

ERROR: configuration failed for package ‘rJava’
* removing ‘/home/darren/R/x86_64-pc-linux-gnu-library/3.2/rJava’
Warning in install.packages :
  installation of package ‘rJava’ had non-zero exit status

So, I went to the terminal and typed,

sudo R CMD javareconf

which also gave the following error,

trying to compile and link a JNI program 
detected JNI cpp flags    : 
detected JNI linker flags : -L/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server -ljvm
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG      -fpic  -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security -D_FORTIFY_SOURCE=2 -g  -c conftest.c -o conftest.o
conftest.c:1:17: fatal error: jni.h: No such file or directory
 #include <jni.h>
                 ^
compilation terminated.
make: *** [conftest.o] Error 1
Unable to compile a JNI program


JAVA_HOME        : /usr/lib/jvm/default-java
Java library path: 
JNI cpp flags    : 
JNI linker flags : 
Updating Java configuration in /usr/lib/R
Done.

I tried to follow these links, one and two but they didn't seem to resolve my issue; there are more links on SO but I'm not sure which one to follow. I've also un-installed and re-installed RStudio via the Ubuntu Software Centre but this didn't make any difference.

Can anyone else help?

In short, I want to be able to use RStudio with rJava again without it destroying any other uses of Java (such as jmol).

Community
  • 1
  • 1
DarrenRhodes
  • 1,431
  • 2
  • 15
  • 29
  • Have you tried sudo apt-get install r-cran-rjava ? – bluefish Dec 10 '15 at 22:21
  • 1
    You should not be searching with the [R] or [rjava] tags but rather searching for how to install java on your machine. – IRTFM Dec 10 '15 at 22:22
  • @bluefish yes, "r-cran-rjava is already the newest version." – DarrenRhodes Dec 10 '15 at 23:04
  • 2
    Yes, as @42- said, you don't even have JDK installed - that's the first thing to do. – Simon Urbanek Dec 11 '15 at 02:33
  • 1
    @42- but when I type, java -version I get the following. java version "1.7.0_91" OpenJDK Runtime Environment (IcedTea 2.6.3) (7u91-2.6.3-0ubuntu0.14.04.1) OpenJDK 64-Bit Server VM (build 24.91-b01, mixed mode) – DarrenRhodes Dec 11 '15 at 18:40
  • So is that also the result when you run `system("java versions")` (from an R console) and are your environment variables set correctly? – IRTFM Dec 11 '15 at 20:11
  • @42- I installed jdk from this link, http://askubuntu.com/questions/56104/how-can-i-install-sun-oracles-proprietary-java-jdk-6-7-8-or-jre (the first jdk answer) and I ran the following in the RStudio console:- system("java versions") Error: Could not find or load main class versions. – DarrenRhodes Dec 11 '15 at 21:13
  • @42- now Jmol doesn't work. Exception in thread "main" java.lang.UnsatisfiedLinkError: /usr/lib/jvm/jdk1.8.0_65/jre/lib/i386/libawt_xawt.so: libXext.so.6: cannot open shared object file: No such file or directory – DarrenRhodes Dec 12 '15 at 00:08
  • The path and envirinment variables for R (or R running in RStudio) is not necessarily the same as you will see when inspecting them with a bash shell. So you need to read about these concerns. A google search should be your first step and it you don't get immediate ecstasy, then post a question on the RStudio help website. – IRTFM Dec 12 '15 at 00:14
  • @42- how can I get my Jmol working again? OK, sudo update-alternatives --config java and setting a 'jre' gets my Jmol working again. – DarrenRhodes Dec 12 '15 at 08:59
  • I've written a short note on this and hope will be helpful: https://zhiyzuo.github.io/installation-rJava/ – Zhiya Apr 13 '18 at 05:08

6 Answers6

36

You don't seem to have JDK installed. You will need at least

sudo apt-get install openjdk-7-jdk

then re-run

sudo R CMD javareconf

Make sure you do NOT set JAVA_HOME by hand - it will be detected automatically. You should then see something like this:

$ sudo R CMD javareconf
Java interpreter : /usr/bin/java
Java version     : 1.7.0_91
Java home path   : /usr/lib/jvm/java-7-openjdk-amd64/jre
Java compiler    : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/bin/jar

trying to compile and link a JNI program 
detected JNI cpp flags    : -I$(JAVA_HOME)/../include
detected JNI linker flags : -L$(JAVA_HOME)/lib/amd64/server -ljvm
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -I/usr/lib/jvm/java-7-openjdk-amd64/jre/../include     -fpic  -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g  -c conftest.c -o conftest.o
gcc -std=gnu99 -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o conftest.so conftest.o -L/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server -ljvm -L/usr/lib/R/lib -lR


JAVA_HOME        : /usr/lib/jvm/java-7-openjdk-amd64/jre
Java library path: $(JAVA_HOME)/lib/amd64/server
JNI cpp flags    : -I$(JAVA_HOME)/../include
JNI linker flags : -L$(JAVA_HOME)/lib/amd64/server -ljvm
Simon Urbanek
  • 13,842
  • 45
  • 45
  • This answer worked and it didn't interfere with my other java based product jmol. Can you edit it with an explanation as to why this worked and others didn't? – DarrenRhodes Dec 20 '15 at 10:22
  • 1
    This solution works because it installs a new JDK in addition to the JDKs of OpenJDK and Oracle that you already have. The command also changes default java to this OpenJDK7, which is compatible to your version of jmol. Other existing JDKs are of Java 8, which are not compatible with jmol. – biocyberman Dec 20 '15 at 21:06
  • 2
    In Debian stretch, I needed to change the jdk installation by `sudo apt-get install default-jdk`and only after that it worked – Adriano Rivolli Aug 28 '18 at 13:47
14

What is wrong with sudo apt-get install r-cran-rjava ?

See for example this earlier answer and the question / thread around it.

For an installation from scratch, you could still much worse than starting from sudo apt-get build-dep r-cran-rjava. It will get you the JDK corresponding to your Ubuntu version.

Community
  • 1
  • 1
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
6

First i would recommend installing Rstudio from its website: https://www.rstudio.com/products/rstudio/download/ (i.e. Rstudio 64bit: https://download1.rstudio.org/rstudio-0.99.489-amd64.deb). This does not solve the problem directly, but it helps to avoid other bugs with Rstudio.

Regarding the error, trying to make sure you have JDK install. I don't think the command java -version can tell if JDK is installed. You have to check the package of JDK itself, or based on the error message, do this:

locate jni.h The output should match or compatible with your JAVAHOME, e.g:

/usr/lib/jvm/java-7-openjdk-amd64/include/jni.h 
/usr/lib/jvm/java-7-oracle/include/jni.h        

Update 1: R CMD javareconf is looking for the jni.h file under $(JAVA_HOME)/include You have JDK installed, but it is very likely that you are having default java to a JRE directory, that why the error happened.

You can see where default-java is really pointing to by doing this command:

jRealDir=$(readlink -f /usr/lib/jvm/default-java)
echo $jRealDir
# sample correct output: /usr/lib/jvm/jdk1.8.0_65 
# or /usr/lib/jvm/java-8-oracle if you default to Oracle's
# now check jni.h
ls -l $jRealDir/include/jni.h
# sample expected output:
# /usr/lib/jvm/jdk1.8.0_65/include/jni.h

If the ls command failed, you have to setup so that javareconf ( and later rJava) can use the java from JDK not from JRE. You have two options:

Method 1: Do it system-wide

This is convenient, but may effect other program like the one you mentioned jmol. But don't worry, this is revertible, just re-run the command and pick the old one. Do the following command and pick the dir that has JDK:

sudo update-alternatives --config java

After that test how jmol works, if it works alright then congrat. You are now ready to test rJava. If not, try the second method below

Method 2: Do it for R only

put this in the .Rprofile under your home directory

Sys.setenv(JAVA_HOME = '/usr/lib/jvm/jdk1.8.0_65')
# this set JAVA_HOME for R to correct java home dir. 

After updating or creating the .Rprofile DO restart R in Rstudio. The R CMD javareconf may still fail in this case, but it should be OK if you run it from Shell under Tools menu of Rstudio.

Regarding the installing or Rstudio from Ubuntu's stock repo. It would not make a difference for getting rJava running. Then again, I recommend installing Rstudio for its homepage because new version also has some nice features (i.e. better autocompletion, which I like the most).

biocyberman
  • 5,675
  • 8
  • 38
  • 50
  • So what do you make of the following? java -version java version "1.8.0_66" Java(TM) SE Runtime Environment (build 1.8.0_66-b17) Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode) and locate jni.h /usr/lib/jvm/java-6-openjdk-amd64/include/jni.h /usr/lib/jvm/java-8-oracle/include/jni.h /usr/lib/jvm/jdk1.8.0_65/include/jni.h and, does it make a difference that I re-installed RStudio via the software centre rather than as you described? – DarrenRhodes Dec 17 '15 at 20:02
  • jRealDir=$(readlink -f /usr/lib/jvm/default-java) echo $jRealDir /usr/lib/jvm/java-7-openjdk-amd64 then ls -l $jRealDir/include/jni.h gives ls: cannot access /usr/lib/jvm/java-7-openjdk-amd64/include/jni.h: No such file or directory. But I tried to get to Method 2 but I couldn't find .Rprofile. I tried ls ~/.R* which gave, /home/darren/.RData /home/darren/.Rhistory Thanks so far ... what next? – DarrenRhodes Dec 18 '15 at 21:25
  • Just create the file from scratch yourself. – biocyberman Dec 18 '15 at 21:27
  • with something like touch ~/.Rprofile followed by vim ~/.Rprofile then within what arises from vim, Sys.setenv(JAVA_HOME = '/usr/lib/jvm/java-7-openjdk-amd64') [note that I've used the results from my system] and save then follow your original instructions for Method 2 (restart etc...)? – DarrenRhodes Dec 18 '15 at 22:10
  • @user1945827 Yes edit the file like you said. HOWEVER, the JAVA_HOME must be set to the dir that has `jni.h`, for example: `/usr/lib/jvm/jdk1.8.0_65`. This one `usr/lib/jvm/java-7-openjdk-amd64` does not have jni.h so you can't use it. – biocyberman Dec 18 '15 at 22:17
  • Hi, can you remind me of where you got /usr/lib/jvm/jdk1.8.0_65 ?? – DarrenRhodes Dec 18 '15 at 23:58
  • It's in the output of "locate" command you post in the first comment of my answer. – biocyberman Dec 19 '15 at 08:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98405/discussion-between-user1945827-and-biocyberman). – DarrenRhodes Dec 19 '15 at 10:14
3

Here is link on R-Bloggers that worked for me: https://www.r-bloggers.com/installing-rjava-on-ubuntu/

sudo apt-get install -y default-jre
sudo apt-get install -y default-jdk
sudo R CMD javareconf
install.packages("rJava")
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
0

I've been dealing with this exact issue, nothing in this thread or other that are similar have solved it. I'm on Ubuntu 16.04, here's how I got it to work:

apt-get install openjdk-9-jdk rm -rf /usr/lib/jvm/default-java ln -s /usr/lib/jvm/java-9-openjdk-amd64/ /usr/lib/jvm/default-java

Wayne Workman
  • 131
  • 1
  • 5
0

You can see where the JAVA_HOME is in the error message.

Then use locate jni.h to find where is jni.h, next use soft link to link this location to $(JAVA_HOME)/include, just like @biocyberman mentioned.

This is what I did:

ln -s /usr/lib/jvm/java-8-openjdk-amd64/include/jni.h /opt/conda/include/jni.h
ln -s /usr/lib/jvm/java-8-openjdk-amd64/include/linux/jni_md.h /opt/conda/include/jni_md.h
ln -s /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server/libjvm.so /usr/lib/

Since my JAVA_HOME is /opt/conda and I also don't have jni_md.h and -ljvm.

I am use Ubuntu 16.04.

Belter
  • 3,573
  • 5
  • 42
  • 58