Using the rJava package on Win7 64 bit with R
Asked Answered
C

13

157

I'm trying to install rJava on a computer with Win 7 64 bit. When I run

install.packages("rJava")

everything seems to be fine:

Installing package(s) into ‘C:/Users/djq/Documents/R/win-library/2.13’
(as ‘lib’ is unspecified)
trying URL 'http://software.rc.fas.harvard.edu/mirrors/R/bin/windows/contrib/2.13/rJava_0.9-1.zip'
Content type 'application/zip' length 654936 bytes (639 Kb)
opened URL
downloaded 639 Kb

package 'rJava' successfully unpacked and MD5 sums checked

The downloaded packages are in
    C:\TEMP\RtmpW2eAUw\downloaded_packages

but when I tried to load the library library(rJava) I get the following 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/me/Documents/R/win-library/2.13/rJava/libs/x64/rJava.dll':
  LoadLibrary failure:  %1 is not a valid Win32 application.

Error: package/namespace load failed for 'rJava'

I have installed the Java JRE (trying both 32 and 64 bit versions) and the JDK (using both 32 and 64 bit). My R version is R version 2.13.1 (2011-07-08) and I'm using RStudio.

If I try using 32 bit R, I can load rJava fine. Is the problem just that rJava does not run in R 64 bit? How can I tell if a package is just for 32 instead of 64?


edit: just posted R.version():

> R.Version()
$platform
[1] "x86_64-pc-mingw32"

$arch
[1] "x86_64"

$os
[1] "mingw32"

$system
[1] "x86_64, mingw32"

$status
[1] ""

$major
[1] "2"

$minor
[1] "13.1"

$year
[1] "2011"

$month
[1] "07"

$day
[1] "08"

$`svn rev`
[1] "56322"

$language
[1] "R"

$version.string
[1] "R version 2.13.1 (2011-07-08)"
Centroclinal answered 11/8, 2011 at 1:37 Comment(3)
Can you please paste the results of R.Version() to your question? I have just installed the 64bit version of rJava using the exact method you describe in your question. My guess is that your version of R is 32 bit.Levinson
pls share your ans how you solved it.Jackstay
Adding a comment here as it may help someone the task of going through the whole solution list. If you get the above error, check if your Java is in Program Files or Program Files (x86). Add path to windows as the accepted answer says. If Program Files (x86), it means you have 32-bit version, follow the link sites.google.com/site/rforfishandwildlifegrads/home/week_2/….Archfiend
P
162

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 in C:\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.

Parang answered 29/9, 2011 at 23:26 Comment(15)
Rather than leaving the second argument blank, you can just do install.packages('rJava', repos='http://www.rforge.net/').Twit
As pointed out in the answer by @user2161065, loading rJava requires removing the JAVA_HOME variableUnific
Although all this might be needed, even after doing this I had to unset JAVA_HOME as suggested by @user2161065Supranatural
package ‘rJava’ is not available (for R version 3.0.2)Pasquinade
Funny thing: After I had to reinstall my computer my own solution https://mcmap.net/q/150940/-using-the-rjava-package-on-win7-64-bit-with-r did not work any longer. Instead I had to add the jvm.dll path to my PATH variable. This is wired!Twyla
Note that this answer assumes that you have a JDK installed. R-Forge says that rJava requires JDK 1.4 or higher.Neusatz
+1 Adding ...\jre\bin\server to path fixed it for meResorcinol
I added C:\Program Files\Java\jdk1.8.0_05\jre\bin\server to path then ran the regular installs install.packages('rJava') and install.packages("RJDBC"). If you try the rforge repos links, the installs will break. I did not have to unset JAVA_HOMEGentilism
Where can we find this "Windows PATH" variable?Gens
@Gens Run the command prompt in Windows and type ECHO %PATH%. You can edit it from here, or go to Control Panel -> System -> Advanced Settings -> Environmental variables to look at a nicer applet.Elly
on win - use user path and add it like this: setx PATH "C:\Program Files\Java\jre1.8.0_102\bin\server;%PATH%"Disclaim
Having had the same problem, I added the path entry as suggested, but find that now (May 2017) install.packages('rJava', .libPaths()[1], 'http://www.rforge.net/') is not working (for me, at least). However a simple install.packages(rJava) and library(rJava) worked after the path change and restart of RStudio.Parulis
to set PATH from R (if windows) Sys.setenv(PATH=paste(Sys.getenv("PATH"), "C:\\your\\path\\to\\jre\\bin\\server_or_client\\", sep = ";"))Brusa
This answer is most accurate. Unsetting Java_Home path leads to error.Sallee
A quick note in 2020: for those using Windows and having issues with rJava (Actually I can load and use the latest rJava package but there is this "unable to load shared object" error when I build my own R package which requires rJava). The solution is: add path in the environment variables, but don't create the system variable "JAVA_HOME" (as some old answers online suggested). When you check Sys.getenv("JAVA_HOME") it should return "".Vivica
T
117

Getting rJava to work depends heavily on your computers configuration:

  1. 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).
  2. 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.

Twyla answered 14/4, 2013 at 11:5 Comment(8)
The previous solutions did not work until I got rid of JAVA_HOME. This is key.Kansas
This JAVA_HOME issue should be reported to rJava as a bug.Nagpur
This really did the trick for me : if (Sys.getenv("JAVA_HOME")!="") Sys.setenv(JAVA_HOME="") library(rJava) Thanks @TwylaSupranatural
adding that script to remove the JAVA_HOME var worked for me!Proctology
1. Thank you very much. Since JAVA_HOME is used by many software other than R, this solution is the best one. 2. I think this problem should be fixed soon as it seems like a bug (I wonder, how it remained there for so long!)Sculptress
This solution also works for packages like XLConnect and XLConnectJars that depend on rJava. (I'm writing this obvious comment so that search engines can pick up on this page more easily than the trouble I had finding it.)Bullroarer
if (Sys.getenv("JAVA_HOME")!="") Sys.setenv(JAVA_HOME=""); library(rJava) is works for me...Falconry
if (Sys.getenv("JAVA_HOME")!="") Sys.setenv(JAVA_HOME="") library(rJava) worked for me..But why Simon Urbanek made such stupid library at least in this case?Licastro
F
32

For me, setting JAVA_HOME did the trick (instead of unsetting, as in another answer given here). Either in Windows:

set JAVA_HOME="C:\Program Files\Java\jre7\"

Or inside R:

Sys.setenv(JAVA_HOME="C:\\Program Files\\Java\\jre7\\")

But what's probably the best solution (since rJava 0.9-4) is overriding within R the Windows JAVA_HOME setting altogether:

options(java.home="C:\\Program Files\\Java\\jre7\\")
library(rJava)
Fiona answered 16/10, 2013 at 21:7 Comment(5)
+1 on overriding the JAVA_HOME env variable and setting in R.Jamille
My JAVA_HOME was set to a JDK folder (as I do Java development as well), and rJava wasn't having it. I used your last fix (options) since I don't want to mess with a nicely working Java dev environment. Thanks!Repeal
options(java.home="my\\path\\to\\jre") worked, when everything else failed, including setting environment PATHCargill
This worked for me: options(java.home="C:\\Program Files\\Java\\jdk1.8.0_144\\jre") . If you have jdk1.8.Incriminate
Using Adopt Open JDK, that final suggestion worked for me. All other popular solutions failed.Sarge
A
8

The last question has an easy answer:

> .Machine$sizeof.pointer
[1] 8

Meaning I am running R64. If I were running 32 bit R it would return 4. Just because you are running a 64 bit OS does not mean you will be running 64 bit R, and from the error message it appears you are not.

EDIT: If the package has binaries, then they are in separate directories. The specifics will depend on the OS. Notice that your LoadLibrary error occurred when it attempted to find the dll in ...rJava/libs/x64/... On my MacOS system the ...rJava/libs/...` folder has 3 subdirectories: i386, ppc, and x86_64. (The ppc files are obviously useless baggage.)

Angloindian answered 11/8, 2011 at 3:40 Comment(1)
My RStudio installation pointed to 64bit R. When I changed it to 32bit, I could import the rJava package. That is why I am so confused about the error message, as it appears to be the exact opposite of what is going on.Centroclinal
S
6

Sorry for necro. I have too run into the same issue and found out that rJava expects JAVA_HOME to point to JRE. If you have JDK installed, most probably your JAVA_HOME points to JDK. My quick solution:

Sys.setenv(JAVA_HOME=paste(Sys.getenv("JAVA_HOME"), "jre", sep="\\"))
Streaming answered 20/7, 2015 at 13:3 Comment(0)
D
4

I had a related problem with rJava. It would load but a package that depends on it, would not load.

Users may waste a lot of time with jvm.dll and PATH and JAVA_HOME when the real fix is to force the installer to just forget about i386. Use option for install.packages. (this also works when drat library is used. (credit goes to Dason)

install.packages("SqlRender",INSTALL_opts="--no-multiarch")

Also, you can modify just your user path with a win command like this:

setx PATH "C:\Program Files\Java\jre1.8.0_102\bin\server;%PATH%"
Disclaim answered 7/7, 2016 at 20:1 Comment(0)
I
3

I had some trouble determining the Java package that was installed when I ran into this problem, since the previous answers didn't exactly work for me. To sort it out, I typed:

Sys.setenv(JAVA_HOME="C:/Program Files/Java/

and then hit tab and the two suggested directories were "jre1.8.0_31/" and "jre7/"

Jre7 didn't solve my problem, but jre1.8.0_31/ did. Final answer was running (before library(rJava)):

Sys.setenv(JAVA_HOME="C:/Program Files/Java/jre1.8.0_31/")

I'm using 64-bit Windows 8.1 Hope this helps someone else.

Update:

Check your version to determine what X should be (mine has changed several times since this post):

Sys.setenv(JAVA_HOME="C:/Program Files/Java/jre1.8.0_x/")
Inspissate answered 4/3, 2015 at 20:17 Comment(0)
C
1
  1. Download Java from https://java.com/en/download/windows-64bit.jsp for 64-bit windows\Install it
  2. Download Java development kit from https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html for 64-bit windows\install it
  3. Then right click on “This PC” icon in desktop\Properties\Advanced system settings\Advanced\Environment Variables\Under System variables select Path\Click Edit\Click on New\Copy and paste paths “C:\Program Files\Java\jdk1.8.0_201\bin” and “C:\Program Files\Java\jre1.8.0_201\bin” (without quote) \OK\OK\OK

Note: jdk1.8.0_201 and jre1.8.0_201 will be changed depending on the version of Java development kit and Java

  1. In Environment Variables window go to User variables for User\Click on New\Put Variable name as “JAVA_HOME” and Variable value as “C:\Program Files\Java\jdk1.8.0_201\bin”\Press OK

To check the installation, open CMD\Type javac\Press Enter and Type java\press enter It will show enter image description here

In RStudio run

Sys.setenv(JAVA_HOME="C:\\Program Files\\Java\\jdk1.8.0_201")

Note: jdk1.8.0_201 will be changed depending on the version of Java development kit

Now you can install and load rJava package without any problem.

Clevis answered 16/1, 2019 at 9:40 Comment(1)
Setting JAVA_HOME via Sys.setenv works for me, in RStudio 1.1.456 on Windows 10 x64; R.Version() 3.5.1; successful rJava and XLConnect pkg. loading.Vivle
I
1

So many answers and yet, nothing worked for me - very disappointing and frustrating.
Every now and then I will run into this issue and waste time on Google.
So here is what DID work for me, I am on Windows 10, with everything fully updated.

  1. I went here to download JDK and JRE 8u301, the latest version at this writing.
    Oracle Latest Java Download Link
    Note I had to create an account or log into my account - takes no time and is free, nothing to worry about.
  2. I installed JRE and then JDK, using the exe just downloaded in the previous step.
  3. In Rstudio (or in R), I ran Sys.setenv(JAVA_HOME='C:\\Program Files\\Java\\jdk1.8.0_301').
  4. Running install.packages("rJava") led to the desired DONE (rJava). (of course, I assume you also have Rtools installed, goes without saying...)

Pretty simple and easy imho. With the right steps, took me 3 min.

Interdental answered 26/9, 2021 at 17:33 Comment(0)
B
0

I need to have a 32 bit JRE available for my browser, but 64 bit JRE for R and rJava. The 32 bit JRE is installed in the default location. After some experimentation, I found that I only needed one of misterbee's suggestions to get rJava (version 0.9-6) working for me. All I did was add the path to my 64 bit java installation:

C:\apps\Java\jre\bin\server\jvm.dll

to the top of my path environment variable (your path will likely be different) and remove my JAVA_HOME as user2161065 suggested. I put this just ahead of the entry

C:\ProgramData\Oracle\Java\javapath

which the Oracle installer inserts at the top of the path and points to some symlinks to the 32 bit JRE. By adding the entry to 64 bit jvm.dll, looks like rJava could find what it needs.

Benny answered 14/7, 2015 at 18:31 Comment(0)
P
0

I think this is an update. I was unable to install rJava (on Windows) until I installed the JDK, as per Javac is not found and javac not working in windows command prompt. The message I was getting was

'javac' is not recognized as an internal or external command, operable program or batch file.

The JDK includes the JRE, and according to https://cran.r-project.org/web/packages/rJava/index.html the current version (0.9-7 published 2015-Jul-29) of rJava

SystemRequirements:     Java JDK 1.2 or higher (for JRI/REngine JDK 1.4 or higher), GNU make

So there you are: if rJava won't install because it can't find javac, and you have the JRE installed, then try the JDK. Also, make sure that JAVA_HOME points to the JDK and not the JRE.

Politick answered 15/9, 2015 at 23:36 Comment(2)
I notice that this is basically opposite of what @Streaming said.Politick
can rJava developer bring some clarity to this problem, plsDisclaim
D
0

I solved the issue by uninstalling apparently redundant Java software from my windows 7 x64 machine. I achieved this by first uninstalling all Java applications and then installing a fresh Java version. (Later I pointed R 3.4.3 x86_64-w64-mingw32 to the Java path, just to mention though I don't think this was the real issue.) Today only Java 8 Update 161 (64-bit) 8.0.1610.12 was left then. After this, install.packages("rJava"); library(rJava) did work perfectly.

Diamagnetism answered 22/2, 2018 at 7:53 Comment(0)
C
0

This is a follow-up to Update (July 2018). I am on 64 bit Windows 10 but am set up to build r packages from source for both 32 and 64 bit with Rtools. My 64 bit jdk is jdk-11.0.2. When I can, I do everything in RStudio. As of March 2019, rjava is tested with <=jdk11, see github issue #157.

  • Install jdks to their default location per Update (July 2018) by @Jeroen.
  • In R studio, set JAVA_HOME to the 64 bit jdk

Sys.setenv(JAVA_HOME="C:/Program Files/Java/jdk-11.0.2")

  • Optionally check your environmental variable

Sys.getenv("JAVA_HOME")

  • Install the package per the github page recommendation

install.packages("rJava",,"http://rforge.net")

FYI, the rstudio scripting console doesn't like the double commas... but it works!

Churchly answered 28/3, 2019 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.