Getting R to use newer versions of java

自作多情 提交于 2019-12-04 07:32:37
m17

Seems like on Mac OS X you can have multiple Java at a one time.

Use below command on terminal to check how many JDK version you have.

/usr/libexec/java_home -V

You can follow below instruction to have correct Java Path setup:

How To Set $JAVA_HOME Environment Variable On Mac OS X

In nutshell do:

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

Unfortunately, none of these seem to help on a Mac. Windows and Linux solutions are not relevant because the files are in different places.

If you just update to Java 1.8 (Java 8 for Oracle) in the standard way as prompted by the Java preferences pain, you just get the Java Runtime Environment (JRE). If you run...

/usr/libexec/java_home -V

...it still shows only java 1.6, and...

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

...throws an error saying it can't find a version 1.8.

To get the Mac to even recognize a newer version of Java, it seems you must install JDK v.8 (not JRE). At that point, you can get the Mac to recognize that a new java virtual machine is available, and you can do the export command successfully. (Note that the new 1.8 JVM is in a DIFFERENT place--/Library/Java instead of /System/Library/Java for 1.6.). BUT, this still does no good for R.

I've tried putting the export JAVA_HOME... command into my .profile and my .bash_profile. Then sourcing both. Works fine, but has no effect on R AFAICT. I've launched R via the standard Mac R GUI, from RStudio, and from the terminal and it is only recognizing Java 1.6. So RNetLogo still does not work.

I will try to find RNetLogo 1.0-0 in the archive and test that. If it works, I suggest that 1.0-1 be rolled back until this Java problem is solved.

I use this line on windows :

options(java.home="C:/Program Files/Java/jre7/")

You probably have to change the 7 for an 8 and find the proper path on the mac.

This supposedly works (originally from this blog entry):

1) Download and install Apple’s Java version 1.6.

2) Reconfigure your Java installation by using sudo R CMD javareconf -n.

3) Reinstall rJava from source with: install.packages('rJava', type='source').

Please acknowledge Will Lowe at conjugateprior.org for the original post and solution.

Try linking libjvm.dylib to /usr/lib:

sudo ln -f -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/lib

-f flag is added to force overwriting existing file/link

EDIT: I don't know if anyone is still struggling with this, but with rJava 0.9-9, the 'partial fix' below no longer works. What does work, and completely, is the final solution offered here: https://github.com/s-u/rJava/issues/86

Copying from there, many thanks to Gregory R. Warnes:

  • uninstall existing rJava versions by running the following in the Terminal:

    Rscript -e 'remove.packages("rJava")'
    sudo Rscript -e 'remove.packages("rJava")'
    
  • add the following to /Users/<userid>/.bashrc:

    export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)/jre'
    

(e.g., type > vim /Users/<userid>/.bashrc in the Terminal, then 'i', add the line above, then ':wq' to save and quit)

  • close and re-start all Terminal, R and RStudio windows

  • type the following in the Terminal window:

    sudo ln -sf $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
    
  • in a new R session, re-install rJava from source:

    install.packages("rJava", repos="http://rforge.net", type="source")
    

OLD 'PARTIAL FIX' BELOW:

Okay. I have been working on this problem all morning, and I have a partial fix.

I tried the solution suggested by Guilherme Kenji Chihaya above, but even after sudo R CMD javareconf -n and install.packages('rJava', type='source'), R insists on using Java 1.6 (and is happy to do so).

HOWEVER, R studio throws an error after re-installing rJava:

library(rJava) Error : .onLoad failed in loadNamespace() for 'rJava', details: call: dyn.load(file, DLLpath = DLLpath, ...) error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.2/Resources/library/rJava/libs/rJava.so': dlopen(/Library/Frameworks/R.framework/Versions/3.2/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib Referenced from: /Library/Frameworks/R.framework/Versions/3.2/Resources/library/rJava/libs/rJava.so Reason: image not found Error: package or namespace load failed for ‘rJava’

Googling this lead me to this post: http://andrewgoldstone.com/blog/2015/02/03/rjava/, with a working solution. Set the following in the Terminal:

alias r="DYLD_FALLBACK_LIBRARY_PATH=/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/server/: open -a r"

And start R from the Terminal. Then, magically, in R:

> library(rJava)
> .jinit()
> .jcall("java/lang/System", "S", "getProperty", "java.runtime.version")
[1] "1.8.0_11-b12"

However, this only works when starting R from the Terminal. I haven't been able to get R to automatically recognise the right "DYLD_FALLBACK_LIBRARY_PATH" in any way.

In Ubuntu there is a command alternatives that i use it for this purpose.

alternatives --install /usr/bin/java java /usr/java/jdk1.8*/jre/bin/java 200000
alternatives --install /usr/bin/javaws javaws /usr/java/jdk1.8*/jre/bin/javaws 200000
alternatives --install /usr/bin/javac javac /usr/java/jdk1.8*/bin/javac 200000
alternatives --install /usr/bin/jar jar /usr/java/jdk1.8*/bin/jar 200000

After alternatives install use the following command to change your version. alternatives --config java and then select your newer java.

If this is not available you should first find out what where is your new java actually. locate *jdk1.8* Then find out which java you run as binary. which java this will returns the path of binary. this is the old java binary, So remove it and link new java binary in the same place. For example : ln -s /path/to/java1.8*/bin/java /usr/bin/java

In addtion you need to update your CLASS_PATH environment variable that is necessary for the VM.

for example: export CLASS_PATH=/usr/java/jdk1.8*/jre/lib and you can add this line in your bashrc file to register this configuration.

In Debian-based installations R uses /etc/R/Makeconf settings for building libraries. One of the setting there is JAVA_HOME. Try setting the correct path there and reinstall the package.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!