MAC安装rJava混乱问题

2019-09-28  本文已影响0人  PriscillaBai

报错如下:

configure: error: Cannot compile a simple JNI program. See config.log for details.
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/3.3/Resources/library/rJava’ * restoring previous ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library/rJava’

主要原因是R,rJava, Java, macOS的内部混乱。当Java遇到了MAC OS,就注定会发生混乱。首先,你需要明白,MAC里的Java有两个版本:Oracle based 或system based。

  1. Java framework reside:
    /System/Library/Frameworks/JavaVM.framework
  2. Oracle based Java will be installed:
    /Library/Java/JavaVirtualMachines

所以说,如果你想在R中合理的配置JAVA,你就必须做一些调整。首先,你必须配置Java_home。

/usr/libexec/java_home -V
Matching Java Virtual Machines (2):
1.8.0_11, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home
1.8.0_05, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home

但是,如果你想在R中用JDK,就会遇到bug了。首先,在R中运行Java:

sudo R CMD javareconf

由于javareconf认定只要你的java_home和"/Home"匹配,就会默认用系统中的java,而不是oracle的java。这时事情就会变得很奇怪了,一半是JDK,一半是苹果系统中的JVM。所以,为了避免这个问题,你需要创造“伪路径”,即没有"/Home$"字符的路径。

cd /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents
ln -s Home RHome

然后, 你将其输出为Java_home

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/RHome

然后,用以下命令配置javareconf

sudo R CMD javareconf
JAVA_HOME={JAVA_HOME} \ JAVA={JAVA_HOME}/bin/java
JAVAC={JAVA_HOME}/bin/javac \ JAVAH={JAVA_HOME}/bin/javah
JAR={JAVA_HOME}/bin/jar \ JAVA_LD_LIBRARY_PATH={JAVA_HOME}/jre/lib/server
JAVA_CPPFLAGS="-I{JAVA_HOME}/include -I{JAVA_HOME}/include/darwin"

现在你就可以测试rJava了。下载rJava包,从source手动配置它。

curl https://cran.r-project.org/src/contrib/rJava_0.9-8.tar.gz -o rJava_0.9-8.tar.gz
tar zxf rJava_0.9-8.tar.gz
cd rJava
./configure

如果通过了,打开R,安装包

install.packages("rJava", type="source")

注意:如果由于Java安装太乱了(Apple’s JREs, Oracle’s JDKs),你需要先设置java的环境。这可能需要更新文件

/usr/libexec/java_home/../Info.plist

如果你想修改文件,就用你喜欢的修改工具吧~

sudo vi /usr/libexec/java_home/../Info.plist

要确认JVM可以处理JNI,你需要将如下:

<key>JVMCapabilities</key>
<array>
<string>CommandLine</string>
</array>

更换成:

<key>JVMCapabilities</key>
<array>
<string>CommandLine</string>
<string>JNI</string>
</array>

注:本帖搬运自http://www.owsiak.org/r-java-rjava-and-macos-adventures/,翻译由于时间关系有点捉急,建议大家看原博客。

关于MAC党的java问题,这个帖子可以说一帖封神了吧。

附上JDK的下载地址:https://www.oracle.com/technetwork/cn/java/javase/downloads/jdk8-downloads-2133151-zhs.html

上一篇下一篇

猜你喜欢

热点阅读