Using the rJava package on Win7 64 bit with R
Update (July 2018):
The latest CRAN version of rJava will find the jvm.dll
automatically, without manually setting the PATH
or JAVA_HOME
. However note that:
- To use rJava in 32-bit R, you need Java for Windows x86
- To use rJava in 64-bit R, you need Java for Windows x64
- To build or check R packages with multi-arch (the default) you need to install both Java For Windows x64 as well as Java for Windows x86. On Win 64, the former installs in
C:\Program files\Java\
and the latter inC:\Program Files (x86)\Java\
so they do not conflict.
As of Java version 9, support for x86 (win32) has been discontinued. Hence the latest working multi-arch setup is to install both jdk-8u172-windows-i586.exe and jdk-8u172-windows-x64.exe and then the binary package from CRAN:
install.packages("rJava")
The binary package from CRAN should pick up on the jvm by itself. Experts only: to build rJava from source, you need the --merge-multiarch
flag:
install.packages('rJava', type = 'source', INSTALL_opts='--merge-multiarch')
Old anwser:
(Note: many of folks in other answers/comments have said to remove JAVA_HOME, so consider that. I have not revisited this issue recently to know if all the steps below are still necessary.)
Here is some quick advice on how to get up and running with R + rJava on Windows 7 64bit. There are several possibilities, but most have fatal flaws. Here is what worked for me:
Add jvm.dll to your PATH
rJava, the R<->Java bridge, will need jvm.dll, but R will have trouble finding that DLL. It resides in a folder like
C:\Program Files\Java\jdk1.6.0_25\jre\bin\server
or
C:\Program Files\Java\jre6\jre\bin\client
Wherever yours is, add that directory to your windows PATH variable. (Windows -> "Path" -> "Edit environment variables to for your account" -> PATH -> edit the value.)
You may already have Java on your PATH. If so you should find the client/server directory in the same Java "home" dir as the one already on your PATH.
To be safe, make sure your architectures match.If you have Java in Program Files
, it is 64-bit, so you ought to run R64. If you have Java in Program Files (x86)
, that's 32-bit, so you use plain 32-bit R.
Re-launch R from the Windows Menu
If R is running, quit.
From the Start Menu , Start R / RGUI, RStudio. This is very important, to make R pick up your PATH changes.
Install rJava 0.9.2.
Earlier versions do not work! Mirrors are not up-to-date, so go to the source at www.rforge.net: http://www.rforge.net/rJava/files/. Note the advice there
“Please use
`install.packages('rJava',,'http://www.rforge.net/')`
to install.”
That is almost correct. This actually works:
install.packages('rJava', .libPaths()[1], 'http://www.rforge.net/')
Watch the punctuation! The mysterious “.libPaths()[1],” just tells R to install the package in the primary library directory. For some reason, leaving the value blank doesn’t work, even though it should default.
Getting rJava to work depends heavily on your computers configuration:
- You have to use the same 32bit or 64bit version for both: R and JDK/JRE. A mixture of this will never work (at least for me).
If you use 64bit version make sure, that you do not set JAVA_HOME as a enviorment variable. If this variable is set, rJava will not work for whatever reason (at least for me). You can check easily within R is JAVA_HOME is set with
Sys.getenv("JAVA_HOME")
If you need to have JAVA_HOME set (e.g. you need it for maven or something else), you could deactivate it within your R-session with the following code before loading rJava:
if (Sys.getenv("JAVA_HOME")!="")
Sys.setenv(JAVA_HOME="")
library(rJava)
This should do the trick in most cases. Furthermore this will fix issue Using the rJava package on Win7 64 bit with R, too. I borrowed the idea of unsetting the enviorment variable from R: rJava package install failing.