how to get rJava 0.9-3 to work on OS X 10.7.4 with Oracle Java 1.7?

This works for me:

The situation is that my Java directories were not configured correctly.

1) First run: sudo R CMD javareconf . It should the following:

Java interpreter : /usr/bin/java
Java version     : 1.7.0_40
Java home path   : /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/jre
Java compiler    : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/bin/jar
Non-system Java on OS X

trying to compile and link a JNI progam 
detected JNI cpp flags    : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/darwin
detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
llvm-gcc-4.2 -arch x86_64 -std=gnu99 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/jre/../include/darwin -I/usr/local/include    -fPIC  -mtune=core2 -g -O2  -c conftest.c -o conftest.o
llvm-gcc-4.2 -arch x86_64 -std=gnu99 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/lib -L/usr/local/lib -o conftest.so conftest.o -L/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/jre/lib/server -ljvm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation


JAVA_HOME        : /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/jre
Java library path: $(JAVA_HOME)/lib/server
JNI cpp flags    : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/darwin
JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
Updating Java configuration in /Library/Frameworks/R.framework/Resources
Done.

2) In R, run: install.packages("rJava",type='source') . Installation should returns DONE(rJava). Note that, I had to set

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home

when I need to start R as root: sudo R. To run as a normal user, use

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/jre

It happened to me that I did not set the symbolic links of the above /usr/bin/java* to the corresponding in Java 1.7 (directed to version 1.6 instead) and got all the frustration from there. Links should be like this: (it looks like only java, javac, javah, and jar are required in R).

$ ls -l /usr/bin/ja*
lrwxr-xr-x  1 root  wheel  71 Oct  3 11:44 /usr/bin/jar -> /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/jar
lrwxr-xr-x  1 root  wheel  77 Oct  3 12:14 /usr/bin/jarsigner -> /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/jarsigner
lrwxr-xr-x  1 root  wheel  72 Oct  3 11:44 /usr/bin/java -> /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/java
lrwxr-xr-x  1 root  wheel  73 Oct  3 11:42 /usr/bin/javac -> /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/javac
lrwxr-xr-x  1 root  wheel  75 Oct  3 12:15 /usr/bin/javadoc -> /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/javadoc
lrwxr-xr-x  1 root  wheel  73 Oct  3 11:44 /usr/bin/javah -> /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/javah
lrwxr-xr-x  1 root  wheel  73 Oct  3 12:15 /usr/bin/javap -> /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/javap
lrwxr-xr-x  1 root  wheel  74 Oct  3 12:15 /usr/bin/javaws -> /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/javaws

Finally, something worked!

Simon Urbanek : can you please put these instructions on the rJava website / cran install instructions...? This is the the first time I've been down this rabbit hole and I'm sure I'm not the only one.

JAVA_LD_LIBRARY_PATH=/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/jre/lib/server JAVA_LIBS='-L/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/jre/lib/server -ljvm' JAVA_CPPFLAGS='-I/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/include/darwin' R CMD javareconf
Java interpreter : /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/jre/bin/java
Java version     : 1.8.0_77
Java home path   : /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home
Java compiler    : /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/bin/javac
Java headers gen.: /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/bin/javah
Java archive tool: /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/bin/jar
System Java on OS X

trying to compile and link a JNI program
detected JNI cpp flags    : -I/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/include/darwin
detected JNI linker flags : -L/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/jre/lib/server -ljvm
clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/include/darwin -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include    -fPIC  -Wall -mtune=core2 -g -O2  -c conftest.c -o conftest.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o conftest.so conftest.o -L/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/jre/lib/server -ljvm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation


JAVA_HOME        : /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home
Java library path: /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/jre/lib/server
JNI cpp flags    : -I/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/include/darwin
JNI linker flags : -L/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/jre/lib/server -ljvm
Updating Java configuration in /Library/Frameworks/R.framework/Resources
override rw-r--r--  root/admin for /Library/Frameworks/R.framework/Resources/etc/Makeconf? (y/n [n]) y
override rw-r--r--  root/admin for /Library/Frameworks/R.framework/Resources/etc/ldpaths? (y/n [n]) y
Done.

R CMD INSTALL rJava_0.9-9.tar.gz