Configure java for rJava inside a conda environment - r

On OSX 10.12.6 I've created a conda environment including R, r-rjava, and java-jdk. Specifically which R and which java point inside the env as expected while the env is activated.
However,
> library(rJava)
> .jinit()
JavaVM: requested Java version ((null)) not available. Using Java at
"" instead. JavaVM: Failed to load JVM:
/bundle/Libraries/libserver.dylib JavaVM FATAL: Failed to load the jvm
library. Error in .jinit() : JNI_GetCreatedJavaVMs returned -1
I see a million related posts, but none exactly with this angle. The first thing I notice is that system("echo $JAVA_HOME") returns the system java home instead of the env java home.
So I tried to fix this with R CMD javareconf. The result is that inside my [env]/lib/R/etc I can see two files got modified (based on the timestamps). Presumably the relevant one is Makeconf, which includes a JAVA_HOME = [env]/jre.
But none of this has any effect on the problem above, and system("echo $JAVA_HOME") still returns the system java home. Any ideas?

Related

error installing xlsx package with object not loaded [duplicate]

While loading rJava package, I receive this error:
Error : .onLoad failed in loadNamespace() for 'rJava', details:
call: inDL(x, as.logical(local), as.logical(now), ...)
error: unable to load shared object 'C:/Users/ankitagarwal5/Documents/R/win-library/3.2/rJava/libs/x64/rJava.dll':
LoadLibrary failure: %1 is not a valid Win32 application.
In addition: Warning message:
package ‘rJava’ was built under R version 3.2.3
Error: package or namespace load failed for ‘rJava’
I referred various posts on StackOverflow and tried to make changes accordingly, but nothing worked. These are the various steps I followed to resolve the issue:
if (Sys.getenv("JAVA_HOME")!="")
Sys.setenv(JAVA_HOME="")
library(rJava)
This didn't work.
Then I tried this:
options(java.home="C:\\Program Files\\Java\\jre1.8.0_91\\")
library(rJava)
This didn't work either.
My jvm.dll is at this location in my Windows 7 64 bit OS - C:\Program Files (x86)\Java\jre1.8.0_91\bin\client
I also added C:\Program Files (x86)\Java\jre1.8.0_91\bin\client to my PATH variable in my Environment variables and restarted RStudio and tried to install and load rJava again. That also didn't work.
I would really appreciate if someone could please help me resolve this issue.
Thanks guys!
This worked for me -
This error is often resolved by installing a Java version (i.e. 64-bit Java or 32-bit Java) that fits to the type of R version that you are using (i.e. 64-bit R or 32-bit R). This problem can easily effect Windows 7 users, since they might have installed a version of Java that is different than the version of R they are using.
Note that it is necessary to ‘manually download and install’ the 64 bit version of JAVA. By default, the download page gives a 32 bit version .
You can pick the exact version of Java you wish to install from this link. If you might (for some reason) work on both versions of R, you can install both version of Java (Installing the “Java Runtime Environment” is probably good enough for your needs).
(Source: Uwe Ligges)
Other possible solutions is trying to re-install rJava.
If that doesn’t work, you could also manually set the directory of your Java location by setting it before loading the library:
Sys.setenv(JAVA_HOME='C:\\Program Files\\Java\\jre7') # for 64-bit version
Sys.setenv(JAVA_HOME='C:\\Program Files (x86)\\Java\\jre7') # for 32-bit version
library(rJava)
Refer this link - http://www.r-bloggers.com/how-to-load-the-rjava-package-after-the-error-java_home-cannot-be-determined-from-the-registry/
I encountered the same problem. Be sure to check the file path for Java. To solve the error I needed to download 64bit Java version. However the following code in R did not work as the folder was incorrect for the latest version for Java:
Sys.setenv(JAVA_HOME='C:\\Program Files\\Java\\jre7') # for 64-bit version
Instead I changed the name to:
Sys.setenv(JAVA_HOME='C:\\Program Files\\Java\\jre1.8.0_144')
Hope that helps
I have noticed in different fresh installations of R, RStudio and rJava in WIndows, that what fixes the error is:
Uninstall R
Reinstall R but only selecting one architecture. Example, x64, or x86, but not both. That tends to confuse R or RStudio when they are compiling.
In my case, I fixed the message
Error : .onLoad failed in loadNamespace() for 'rJava', details:
call: inDL(x, as.logical(local), as.logical(now), ...)
error: unable to load shared object 'C:/Users/ankitagarwal5/Documents/R/win-library/3.2/rJava/libs/x64/rJava.dll':
LoadLibrary failure: %1 is not a valid Win32 application.
Following steps 1 and 2 above.
I tried what the other have said to not avail. For me the solution was to eliminate the old folders and shortcuts of R version 32 bit on my computer that remained there even after uninstalling.
This happened to me when Java was updated to a 32-bit, but R required a 64-bit version.
The solution:
Simply download the 64-bit version of Java
install it.
Restart RStudio
Everything should work
Where to download 64-bit java:
I guess your OS architecture and R architecture are not matching. If they are matching, the following might be the problem
It worked for me after i added the R library path to the environment variables list on my windows 7.
So,
Add 'C:/Users/Rohan-PC/Documents/R/win-library/3.1' this path to the Path variables list under System variables list(not to be confused with the PATH variable under User variables). Restart R and try loading the library. It works!!!!!
If you have both 64-bit R and 32-bit R versions installed, try from each one.
For me it worked on R 64bits but not R 32bits.
But of course that depends on your java version (64-bit Java vs 32-bit Java).

jni.h file not found

I'm ultimately trying to install the RJDBC package on a mac running R 3.3.3 and Java 12 and Mojave OS.
When trying to install RDJBC I get the following error:
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.
So then I go ahead and run R CMD javareconf in the terminal and I get the following error:
conftest.c:1:10: fatal error: 'jni.h' file not found
I've found various questions online around the same topic and have tried out suggested solutions and none have worked for me. Ity could be a combination of my OS/Java version. Would someone be willing to walk me through a fix here?
Update:
Stacktrace
WARNING: Initial Java 12 release has broken JNI support and does NOT work. Use stable Java 11 (or watch for 12u if avaiable).
ERROR: Java exception occurred during rJava bootstrap - see stderr for Java stack trace.
Exception in thread "main" java.lang.NullPointerException
at java.base/jdk.internal.reflect.Reflection.verifyMemberAccess(Reflection.java:130)
at java.base/java.lang.reflect.AccessibleObject.slowVerifyAccess(AccessibleObject.java:673)
at java.base/java.lang.reflect.AccessibleObject.verifyAccess(AccessibleObject.java:666)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:638)
at java.base/java.lang.reflect.Field.checkAccess(Field.java:1075)
at java.base/java.lang.reflect.Field.get(Field.java:416)
Error in .jcheck(silent = FALSE) :
java.lang.NullPointerException.jcall(f, "Ljava/lang/Object;", "get", .jcast(ic, "java/lang/Object"))new("jobjRef", jobj = <pointer: 0x7fd4a04ac4b8>, jclass = "java/lang/NullPointerException")
Interestingly, it seems to work when I run R through terminal, but not in Rstudio.
This will need some tweaking. In general, R, Java and rJava are not quite easy things to setup when they are supposed to work together. Make sure to go through: http://www.owsiak.org/r-java-11-and-making-sure-you-can-load-rjava/
Note that due to changes in Java 12 it is no longer possible to dynamically affect location of shared libraries via reflection - this affects rJava. I am pretty sure you won't get rJava and any other JNI based codes working properly with Java 12 in R.
Once you have your R configured, it will work as expected
> R
R version 3.6.1 (2019-07-05) -- "Action of the Toes"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)
...
...
> install.packages("RJDBC")
--- Please select a CRAN mirror for use in this session ---
...
> library(RJDBC)
Loading required package: DBI
>
Update
For Java 12 try following. Inside ~/.profile add following line
export JAVA_HOME=$(/usr/libexec/java_home -v 12)
then, run
> sudo R CMD javareconf \
JAVA_HOME=${JAVA_HOME} \
JAVA=${JAVA_HOME}/bin/java \
JAVAC=${JAVA_HOME}/bin/javac \
JAVAH=${JAVA_HOME}/bin/javah \
JAR=${JAVA_HOME}/bin/jar
and, inside R
> system("java -version")
java version "12.0.1" 2019-04-16
Java(TM) SE Runtime Environment (build 12.0.1+12)
Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)
> library(RJDBC)
Loading required package: DBI
Loading required package: rJava
>
Of course, you still need XCode.app as it contains headers for JNI.
Update
In order to make sure all the apps are getting Java 11 instead of Java 12 (not only CLI based) you can "disable" Java 12 following way.
cd /Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents
sudo mv Info.plist Info.plist~
Since now, even though you haven't removed Java 12, RStudio will pick up correct version.

Setting up macOS terminal to start a non-Anaconda R installation?

I'm actually trying to get R work with the Jupyter notebook. For this the IRkernel is the native kernel but I'm getting an error code 127 when executing the last step. This is because the last command IRkernel::installspec() needs to be run in bash but the terminal is not recognising a valid PATH to a R installation.
Now, I also have Anaconda installed and despite providing the terminal a valid path to R, keying-in 'R' in the terminal returns this error:
MacBook-Pro:~ Shiv$ R
dyld: Library not loaded: #rpath/libintl.9.dylib
Referenced from: /Users/Shiv/anaconda3/lib/R/lib/libR.dylib
Reason: image not found
Abort trap: 6
Which I believe its pointing out to my Anaconda libraries that doesn't have RStudio installed. Now, I would like to keep R as a totally separate tool and not as a part of Conda. What am I missing here that bash won't start R in the native shell? Once am through with it, installing the kernel to work with Jupyter is a tiny process.

Getting gputools and gmatrix in R to run smoothly on Linux

I am having trouble getting R to recognize gmatrix for R, running through Rstudio. My details are below.
Things that are accomplished and DONE:
install of openSuse Tumbleweed x86_64
install of NVIDIA graphics driver 361.42
install of CUDA 7.5 & symbolic link for /usr/local/cuda
set LD_LIBRARY_PATH & PATH
R 3.2.5
RStudio 0.98.501-1.62
patched /usr/local/cuda/include/host_config.h, line: 115 by increasing the "4"'s to "5"'s (this pertains to the gcc compiler)
install package gputools
~ partial install of package gmatrix
*I had to edit the host_config.h file because cuda is not supported for the gcc compiler that Tumbleweed ships with. I basically broke the evaluation that checks for version and set it to 5.9 instead of 4.9. Was this a supremely naive thing to do?
Things not Done, help required:
-I wasn't able to install gmatrix properly. I got R to install by manually adding the LD & PATH directories using the Sys.setenv()
> Sys.setenv("LD_LIBRARY_PATH"="/usr/lib64/R/lib:/lib:/usr/local/lib64:/usr/local/cuda/lib64")
> Sys.setenv("PATH"="/home/eamergent/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:/usr/local/cuda/bin")
However, I am unable to load the gmatrix package, yet gputools works fine:
> library(gputools) "no issues"
> library(gmatrix)
> Error in dyn.load(file, DLLpath = DLLpath, ...) : unable to load shared object '/home/eamergent/R/x86_64-suse-linux-gnu-library/3.2/gmatrix/libs/gmatrix.so':libcudart.so.7.5: cannot open shared object file: No such file or directory Error: package or namespace load failed for ‘gmatrix’
*The Error makes it seem that libcudart.so.7.5 is to blame
However,
/usr/local/cuda/lib64/libcudart.so.7.5 does in fact exist:
> eamergent#linux-pnx7: ls /usr/local/cuda/lib64/libcuda
libcudadevrt.a libcudart.so libcudart.so.7.5 libcudart.so.7.5.18 libcudart_static.a
*I used tab to get a shorter list. there are many more in lib64
Any ideas on how to get R to recognize the already installed gmatrix package? Or how to properly reinstall without using the R environment path fix that I used above?
I had the same issue on Ubuntu 14.04 where the system and R (Sys.setenv()) shared library paths were set properly, but R failed to load the CUDA shared library. What worked was to set the CUDA shared library path using ldconfig.
as root or prefix commands with sudo:
# echo "/usr/local/cuda/lib64/libcuda" > /etc/ld.so.conf.d/cuda.conf
# ldconfig
then
# R
> install.packages("gmatrix")
or if gmatrix is already installed
# R
> library(gmatrix)
> gtest()

How can I install rJava for use with 64bit R on a 64 bit Windows computer?

I installed iplots and rjava packages. When I do this library(iplots), I get the following error. I do have JDK installed on my pc.
Loading required package: rJava
Error : .onLoad failed in loadNamespace() for 'rJava', details:
call: fun(libname, pkgname)
error: JAVA_HOME cannot be determined from the Registry
Error: package ‘rJava’ could not be loaded
The error is telling you that there is no entry in your Registry that tells R where Java is located on your machine. Either your registry is corrupt, but more likely you haven't installed Java. You can install either the Java Runtime Environment or the Java Development Kit.
(You can download Java here.)
If you installed Java, try reinstalling it. This should put the entries back in your Registry.
If that doesn't work, you can start looking at exactly where R is looking for your Registry entries. The function that rJava uses to find Java is in the rJava:::.onLoad function. Within that function there is a subfunction called find.java. I copy the contents here:
find.java <- function() {
for (root in c("HLM", "HCU")) for (key in c("Software\\JavaSoft\\Java Runtime Environment",
"Software\\JavaSoft\\Java Development Kit")) {
hive <- try(utils::readRegistry(key, root, 2),
silent = TRUE)
if (!inherits(hive, "try-error"))
return(hive)
}
hive
}
Copy and paste this into your R window, and then run it find.java(). rJava is looking for an entry for JavaHome. If that isn't listed, then it is missing from your registry.
You could also manually set the directory of your Java location by setting it before loading the library:
Sys.setenv(JAVA_HOME='C:\\Your\\Java\\Directory')
library(rJava)
If like me you do not have admin rights to install 64-bit Java, just open 32-bit R and it should work ok on your 64 bit PC as part of the problem seems to be the rJava library function calls embedded Java functions/routines, which may only have being designed for 32-bit interface with Excel/Windows and possibly too large a task at the time to change everything.
Answer in link resolved my issue.
Before resolution, I tried by adding JAVA_HOME to windows environments. It resolved this error but created another issue. The solution in above link resolves this issue without creating additional issues.
Any Linux users here, run command:
sudo R CMD javareconf
That typically needs to be run after an update of the system Java installation,
as suggested here

Resources