Overview

最近把系统更新到了最新版本(10.12.3),又安装了一个新的语言Julia,再使用R的时候,发现命令行已经不能通过R命令进入了,连*.R文件都不再能被R语言关联了。具体原因未知,于是先下了一个最新的R-3.3.3,顺带删了Julia,所有的library都要重新安装,结果安装好xlsx后导入的时候报了错。
各种解决方法都尝试了,还是不行。考虑到可能是R的版本太新了,就把R-3.3.3卸载了,重新装了一个R-3.2.3版本,依然存在同样的问题,又过了一遍解决方法,解决了。

问题描述

> library("xlsx")
载入需要的程辑包:xlsxjars
JavaVM: requested Java version ((null)) not available. Using Java at "" instead.
JavaVM: Failed to load JVM: /bundle/Libraries/libserver.dylib
JavaVM FATAL: Failed to load the jvm library.
Error : loadNamespace()里算'xlsx'时.onLoad失败了,详细内容:
  调用: .jinit()
  错误: JNI_GetCreatedJavaVMs returned -1

错误: ‘xlsx’程辑包或名字空间载入失败,

实际上,载入xlsx需要载入rJavaxlsxjars两个包,单独载入rJavaxlsxjars时都不会报错,只有载入xlsx时才会报错。

配置环境

用下面的命令查看自己的配置环境:

  • 操作系统版本:

    > sw_vers
    ProductName:    Mac OS X
    ProductVersion: 10.12.3
    BuildVersion:   16D32
    
  • Java版本:

    > java -version
    java version "1.8.0_25"
    Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
    Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
    
  • Java路径:

    > which java
     /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/java
    
  • JAVA HOME:

    > /usr/libexec/java_home
    /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
    
  • R 版本:

    >R --version
    R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree"
    Copyright (C) 2015 The R Foundation for Statistical Computing
    Platform: x86_64-apple-darwin13.4.0 (64-bit)
    
  • R 配置情况:

    >sudo R CMD javareconf
    Java interpreter : /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/java
    Java version     : 1.8.0_25
    Java home path   : /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre
    Java compiler    : /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/javac
    Java headers gen.: /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/javah
    Java archive tool: /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/jar
    Non-system Java on OS X
    
    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/jdk1.8.0_25.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/../include/darwin -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include    -fPIC  -Wall -mtune=core2 -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/jdk1.8.0_25.jdk/Contents/Home/jre/lib/server -ljvm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
    
    
    JAVA_HOME        : /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre
    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.    
    

    这个命令最好要用sudo执行,否则可能会在配置的过程中出现权限问题,报下面的错误:

    Java interpreter :     /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/bin/java
    Java version     : 1.8.0_25
    Java home path   :     /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
    Java compiler    :     /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/javac
    Java headers gen.:     /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/javah
    Java archive tool:     /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/jar
    System Java on OS X
    
    trying to compile and link a JNI program
    detected JNI cpp flags    : -    I/System/Library/Frameworks/JavaVM.framework/Headers
    detected JNI linker flags : -framework JavaVM
    clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/System/Library/Frameworks/JavaVM.framework/Headers -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include    -fPIC  -Wall -mtune=core2 -g -O2  -c conftest.c -o conftest.o
    conftest.c:1:10: fatal error: 'jni.h' file not found
    #include <jni.h>
         ^
    1 error generated.
    make: *** [conftest.o] Error 1
    Unable to compile a JNI program
    

解决方法

报错信息其实很明显,就是找不到jvm的位置。我花了很久的时间重新配置了JAVA_HOME,依然不行,最后发现是因为rJava包的安装问题。

  • 在linux shell命令行,执行下面的命令(其实刚才检查的时候已经执行过了):
    sudo R CMD javareconf
    
  • 打开R命令行(最好在sudo下打开),用下面的命令重新安装rJava包:
    install.packages("rJava",type='source')
    

重新载入rJava包,就可以了。

一点小问题

之前的问题是解决了,但是会有点后遗症:以后无论是在启用R命令行或者运行R语言脚本的时候,如果要导入rJava包,都需要使用sudo命令,否则就会报下面的错误:

Error : loadNamespace()里算'rJava'时.onLoad失败了,详细内容:
  调用: dyn.load(file, DLLpath = DLLpath, ...)
  错误: 无法载入共享目标对象‘/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
错误: ‘rJava’程辑包或名字空间载入失败,

因为没有sudo权限,没办法访问/Library/Frameworks/R.framework/Versions/3.2/Resources/library/rJava/libs/rJava.so。暂时没有更好的解决方案,找到之后再加上。

参考网址