How can I make rJava use the newer version of java on osx?

前端 未结 4 2186
粉色の甜心
粉色の甜心 2020-12-01 18:16

I am following this tutorial on rJava: http://cran.r-project.org/web/packages/helloJavaWorld/vignettes/helloJavaWorld.pdf

I have made all the files as specified in t

4条回答
  •  青春惊慌失措
    2020-12-01 19:02

    I had a similar problem, but this solution didn't work for me. I eventually got it working, but now I'm not sure which of the things I changed solved the problem. Here's what I did:

    1. I added the following lines to my .bash_profile:

      export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre"
      export LD_LIBRARY_PATH=/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/server 
      export PATH=$PATH:$JAVA_HOME/bin
      
    2. I set my java.home option and my DYLD_FALLBACK_LIBRARY_PATH environmental variable in R:

      options(java.home="/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk")
      Sys.setenv(DYLD_FALLBACK_LIBRARY_PATH="/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/server/")
      
    3. I re-installed Apple's version of Java 1.6 just in case, as per this issue thread: https://github.com/s-u/rJava/issues/37

    4. I reconfigured R's java settings. In Bash: R CMD javareconf
    5. I ran R as root, and then installed rJava from source, directly from RForge. In Bash: sudo R. Then in the R session: install.packages('rJava',,'http://www.rforge.net/').

    Installing from source was crucial to making rJava pick up on R's new Java settings. When I didn't install from source, rJava would install ok, but it would continue to use JRE 1.6. You can check which JRE rJava uses by running:

    library(rJava)
    .jinit()
    .jcall("java/lang/System", "S", "getProperty", "java.runtime.version")
    

    Running R as root was crucial to making rJava install correctly from the RForge source. When I tried to run install.packages('rJava',,'http://www.rforge.net/') as User, I got the following error messages:

    • If running R in the command line, rJava would not install correctly:

      checking JNI data types... configure: error: One or more JNI types differ from the corresponding native type. You may need to use non-standard compiler flags or a different compiler in order to fix this.
      ERROR: configuration failed for package ‘rJava’
      
    • If running R as an application from Finder, rJava would install, but not load:

      > 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’
      

    Hopefully this answer will save someone else some time!

    Edited to add: Two of my professors followed these instructions and ran into the following issue: rJava would work when running R in the command line, but would fail to load when running RStudio or the default Mac R app.

    Joe Ramsey figured out the solution. He writes:

    Apparently RStudio grumbles about having to use the default user/directory that Apple uses to open applications.

    This article describes it: http://jeromyanglim.tumblr.com/post/34221143729/how-to-run-rstudio-from-the-command-line-on-osx

    [To fix the issue] You go to the command line and type: open -a rstudio or open -a R

    Edit number two: I just installed rJava on one of the school computers, running Ubuntu 14.04.4 LTS (64-bit). I was able to install rJava when running R as root. However, when I then tried to run R as user and load the package, I got a brand new loading error:

    > library(rJava)
    Error : .onLoad failed in loadNamespace() for 'rJava', details:
        call: dyn.load(file, DLLpath = DLLpath, ...)
        error: unable to load shared object '/home/lizziesilver/R/x86_64-pc-linux-gnu-library/3.2/rJava/libs/rJava.so':
        libjvm.so: cannot open shared object file: No such file or directory
      Error: package or namespace load failed for ‘rJava’
    

    I checked the directory; rJava.so definitely existed. It turned out that I didn't have the right permissions for it:

    ...$ ls -l /home/lizziesilver/R/x86_64-pc-linux-gnu-library/3.2/rJava/libs/rJava.so
    -rwxr-xr-x 1 root root 353325 Feb 26 16:58 /usr/lib/R/library/rJava/libs/rJava.so
    

    So I changed the permissions: sudo chmod -R a+rX /home/lizziesilver/R/x86_64-pc-linux-gnu-library/3.2/rJava/

    Then reconfigured R's java settings:

    export LD_LIBRARY_PATH=/usr/lib/jvm/java-8-oracle/lib/amd64:/usr/lib/jvm/java-8-oracle/jre/lib/amd64/server
    sudo R CMD javareconf
    

    Now rJava loads, even when running R as user instead of root!

    rJava: the package that keeps on giving (config errors)

提交回复
热议问题