Search code examples
rmacosrjava

R: command not found


I want to install rJava but this fails with the following suggestion:

Make sure you have Java Development Kit installed and correctly registered in R.
If in doubt, re-run "R CMD javareconf" as root.

ERROR: configuration failed for package ‘rJava’
* removing ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rJava’
* restoring previous 
‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rJava’

Hence, I follow this suggestion (and the one everyone else seems to suggest) and run R CMD javereconf in the terminal but now I get the message that zsh: command not found: R.

How can I get R CMD javereconf to work? Thanks.

EDIT: While I followed the blogpost suggested by @Till, I still struggle to run R CMD javereconf (same error). In the meantime, I figured that I should mention that I'm using MacOS Big Sur with an Apple M1 Chip. When typing R.home() RStudio returns /Library/Frameworks/R.framework/Resources/R.


Solution

  • There is currently a bug in CRAN's R installation package that results in it not correctly installing symbolic links to R and Rscript for commandline use. I've just verified this by inspecting the postflight script in their 4.0.5 installation package.

    Basically, there's a problem wherein the uname check doesn't know about operating system releases of 20 and above.

    If your uname -r release version is over 20, this would explain why the installation silently failed. My recommendation would be to manually create the symbolic links the package is supposed to create by doing something like this:

    if uname -r | grep '^2' >/dev/null; then
        ## 15.0 and higher don't allow messing with /usr/bin
        ## so use /usr/local/bin instead
        if [ ! -e /usr/local/bin ]; then
            mkdir -p /usr/local/bin
        fi
     
        cd /usr/local/bin
    
        # create convenience links to R and Rscript
        rm -f R Rscript
        ln -s /Library/Frameworks/R.framework/Resources/bin/R .
        ln -s /Library/Frameworks/R.framework/Resources/bin/Rscript .
    fi
    

    I'm going to file a bug with R-project shortly.