无法在MacOS Mojave和R 3.5.2上加载rJava

时间:2019-03-08 04:10:20

标签: r macos rjava

我问过a similar question before,但没有得到我可以实际实现的任何答案。我还阅读了herehere的答案,但是无法实现其中的任何一个(部分原因是没有答案实际上可以追溯其所有步骤。

问题

尝试在R 3.5.2和MacOS Mojave 10.14.3上加载rJava时出现错误:

$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home

尝试的解决方案

我首先尝试了this solution,但没有成功。然后,基于this answer,我尝试了以下操作:

在终端上输入以下内容:

> options("java.home"="/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home")
> 
> Sys.setenv(LD_LIBRARY_PATH='$JAVA_HOME/server')
> 
> dyn.load('/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/lib/server/libjvm.dylib')
> 
> library(rJava)
Error: package or namespace load failed for ‘rJava’:
 .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.5/Resources/library/rJava/libs/rJava.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
  Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so
  Reason: image not found

然后在R上尝试以下操作:

> options("java.home"="/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/bin")
> Sys.setenv(LD_LIBRARY_PATH='$JAVA_HOME/server')
> dyn.load('/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/lib/server/libjvm.dylib')
> library(rJava)
Error: package or namespace load failed for ‘rJava’:
 .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.5/Resources/library/rJava/libs/rJava.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
  Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so
  Reason: image not found

我也尝试过:

encrypt(plaintxt){
    const secretKey = '12456780123456';
    const iv = "124567890123456";

    let a = "awal";
    AesCrypto.encrypt(plaintxt,secretKey,iv).then(cipher=>{
        a = cipher;
    }).catch(err=>{
        a = err;
    });
    return a;
}

过去,我已经成功降级到R 3.1,但是我不想每次我都想使用rJava时都要这么做

2 个答案:

答案 0 :(得分:1)

我有类似的问题。如果您尝试了所有这些解决方案但没有任何效果,那么从头开始通常是最好的选择。您不希望这些“修复程序”之一引起更多问题,因为其中大多数都需要您更改环境变量。

这是我修复它的方式。

  1. 安装最新的JAVA JDK https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

  2. 在终端中运行ServiceClass。请勿自行更改任何环境变量。

希望有帮助。

答案 1 :(得分:1)

这是适合我的设置

  1. macOS Mojave-10.14.3

  2. XQuartz-版本2.7.11-https://www.xquartz.org

enter image description here

  1. R-版本5.3.2

    > R --version
    R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
    Copyright (C) 2018 The R Foundation for Statistical Computing
    Platform: x86_64-apple-darwin15.6.0 (64-bit)
    
    R is free software and comes with ABSOLUTELY NO WARRANTY.
    You are welcome to redistribute it under the terms of the
    GNU General Public License versions 2 or 3.
    For more information about these matters see
    http://www.gnu.org/licenses/.
    
  2. Java-11.0.1

    > java -version
    java version "11.0.1" 2018-10-16 LTS
    Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
    Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
    
  3. rJava-从CRAN安装

    > > install.packages("rJava")
    --- Please select a CRAN mirror for use in this session ---
    Fontconfig warning: ignoring UTF-8: not a valid region tag
    trying URL 'https://cloud.r-project.org/bin/macosx/el-         
    capitan/contrib/3.5/rJava_0.9-10.tgz'
    Content type 'application/x-gzip' length 739259 bytes (721 KB)
    ==================================================
    downloaded 721 KB
    
    
    The downloaded binary packages are in
    /var/folders/...
    > library(rJava)
    >
    

更新

在您的情况下,似乎缺少在rJava的安装/编译过程中使用的JDK。您可以从消息中得知:

dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so
Reason: image not found

确保此文件存在:

/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib

可能丢失了。您在这里有两个选择:安装Java 11.0.1,重新配置R。

您可以使用/usr/libexec/java_home -V

列出所有可用的JVM安装。

再现最初的问题,并加以解决

  1. 让我们假装我们已删除JDK 11.0.1

    > pwd /Library/Java/JavaVirtualMachines
    > tree -L 1
    .
    |-- jdk-11.0.1.jdk~
    `-- jdk-11.0.2.jdk
    > java -version
    java version "11.0.2" 2019-01-15 LTS
    Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)
    Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)
    
  2. 让我们尝试加载rJava

    > R
    
    R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
    Copyright (C) 2018 The R Foundation for Statistical Computing
    Platform: x86_64-apple-darwin15.6.0 (64-bit)
    
    ...
    ...
    ...
    
    > library(rJava)
    Error: package or namespace load failed for ‘rJava’:
     .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.5/Resources/library/rJava/libs/rJava.so':
      dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
      Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so
      Reason: image not found
    
  3. 让我们修复

    > sudo R CMD javareconf
    Java interpreter : /usr/bin/java
    Java version     : 11.0.2
    Java home path   : /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
    Java compiler    : /usr/bin/javac
    Java headers gen.: /usr/bin/javah
    Java archive tool: /usr/bin/jar
    
    trying to compile and link a JNI program
    detected JNI cpp flags    : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/darwin
    detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
    clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/include/darwin  -I/usr/local/include   -fPIC  -Wall -g -O2  -c conftest.c -o conftest.o
    clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o conftest.so conftest.o -L/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/lib/server -ljvm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
    
    
    JAVA_HOME        : /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
    Java library path: $(JAVA_HOME)/lib/server
    JNI cpp flags    : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/darwin
    JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
    Updating Java configuration in /Library/Frameworks/R.framework/Resources
    Done.
    
  4. 现在应该可以正常工作

    > R
    
    R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
    Copyright (C) 2018 The R Foundation for Statistical Computing
    Platform: x86_64-apple-darwin15.6.0 (64-bit)
    
    ...
    ...
    ...
    
    > library(rJava)
    > quit()