如何让rJava在osx上使用较新版本的java?

2022-09-03 17:59:56

我正在rJava上遵循本教程:http://cran.r-project.org/web/packages/helloJavaWorld/vignettes/helloJavaWorld.pdf

我已经按照教程中指定的所有文件并安装了helloJavaWorld包,但是一旦我运行helloJavaWorld()函数,它就会抱怨:

> helloJavaWorld()
Error in .jnew("HelloJavaWorld") :
  java.lang.UnsupportedClassVersionError: HelloJavaWorld : Unsupported major.minor version 52.0

所以我试图检查rJava正在使用的java版本:

.jinit()
jvm = .jnew("java.lang.System")
jvm.props = jvm$getProperties()$toString()
jvm.props <- strsplit(gsub("\\{(.*)}", "\\1", jvm.props), ", ")[[1]]
jvm.props

 [1] "java.runtime.name=Java(TM) SE Runtime Environment"
 [2] "sun.boot.library.path=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Libraries"
 [3] "java.vm.version=20.65-b04-466.1"
 [4] "awt.nativeDoubleBuffering=true"
 [5] "gopherProxySet=false"
 [6] "mrj.build=11M4716"
 [7] "java.vm.vendor=Apple Inc."
 [8] "java.vendor.url=http://www.apple.com/"
 [9] "path.separator=:"
[10] "java.vm.name=Java HotSpot(TM) 64-Bit Server VM"
[11] "file.encoding.pkg=sun.io"
[12] "user.country=US"
[13] "sun.os.patch.level=unknown"
[14] "java.vm.specification.name=Java Virtual Machine Specification"
[15] "user.dir=/private/tmp"
[16] "java.runtime.version=1.6.0_65-b14-466.1-11M4716"
[17] "java.awt.graphicsenv=apple.awt.CGraphicsEnvironment"
[18] "java.endorsed.dirs=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/endorsed"
[19] "os.arch=x86_64"
[20] "java.io.tmpdir=/var/folders/5d/44ctbbln4dsflgzxph1dm8wr0000gn/T/"
[21] "line.separator=\n"
[22] "java.vm.specification.vendor=Sun Microsystems Inc."
[23] "os.name=Mac OS X"
[24] "sun.jnu.encoding=MacRoman"
[25] "java.library.path=.:/Users/kaiyin/Library/Java/Extensions:/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java"
[26] "java.specification.name=Java Platform API Specification"
[27] "java.class.version=50.0"
[28] "sun.management.compiler=HotSpot 64-Bit Tiered Compilers"
[29] "os.version=10.10"
[30] "http.nonProxyHosts=local|*.local|169.254/16|*.169.254/16"
[31] "user.home=/Users/kaiyin"
[32] "user.timezone="
[33] "java.awt.printerjob=apple.awt.CPrinterJob"
[34] "file.encoding=MacRoman"
[35] "java.specification.version=1.6"
[36] "java.class.path=/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rJava/java/boot:/Library/Frameworks/R.framework/Versions/3.1/Resources/library/helloJavaWorld/java"
[37] "user.name=kaiyin"
[38] "java.vm.specification.version=1.0"
[39] "java.home=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home"
[40] "sun.arch.data.model=64"
[41] "user.language=en"
[42] "java.specification.vendor=Sun Microsystems Inc."
[43] "awt.toolkit=apple.awt.CToolkit"
[44] "java.vm.info=mixed mode"
[45] "java.version=1.6.0_65"
[46] "java.ext.dirs=/Users/kaiyin/Library/Java/Extensions:/Library/Java/Extensions:/System/Library/Java/Extensions:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext"
[47] "sun.boot.class.path=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jsfd.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar:/System/Library/Frameworks/JavaVM.framework/Frameworks/JavaRuntimeSupport.framework/Resources/Java/JavaRuntimeSupport.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/ui.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/laf.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/sunrsasign.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jsse.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jce.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/charsets.jar"
[48] "java.awt.headless=true"
[49] "java.vendor=Apple Inc."
[50] "file.separator=/"
[51] "java.vendor.url.bug=http://bugreport.apple.com/"
[52] "sun.io.unicode.encoding=UnicodeLittle"
[53] "sun.cpu.endian=little"
[54] "mrj.version=1070.1.6.0_65-466.1"
[55] "socksNonProxyHosts=local|*.local|169.254/16|*.169.254/16"
[56] "ftp.nonProxyHosts=local|*.local|169.254/16|*.169.254/16"
[57] "sun.cpu.isalist="

它看起来像是使用java 1.6(预装在OSX上),但是我用java 1.8编译了HelloJavaWorld类,这就引出了一个问题:我如何让rJava使用较新版本的java?

输出 :R CMD javareconf

Java interpreter : /usr/bin/java
Java version     : 1.8.0_25
Java home path   : /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre
Java compiler    : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/bin/jar
Non-system Java on OS X

trying to compile and link a JNI progam
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/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.

内容 /Library/Frameworks/R.framework/Versions/3.1/Resources/etc/javaconf

## Versions from settings when configure was run
: ${JAVA_HOME=}
: ${JAVA_CPPFLAGS=~autodetect~}
: ${JAVA_LD_LIBRARY_PATH=~autodetect~}
: ${JAVA_LIBS=~autodetect~}

/Library/Frameworks/R.framework/Versions/3.1/Resources/etc/ldpaths 的内容

➜  etc  cat ldpaths
: ${JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre}
: ${R_JAVA_LD_LIBRARY_PATH=${JAVA_HOME}/lib/server}
if test -n ""; then
: ${R_LD_LIBRARY_PATH=${R_HOME}/lib:}
else
: ${R_LD_LIBRARY_PATH=${R_HOME}/lib}
fi
if test -n "${R_JAVA_LD_LIBRARY_PATH}"; then
  R_LD_LIBRARY_PATH="${R_LD_LIBRARY_PATH}:${R_JAVA_LD_LIBRARY_PATH}"
fi
## This is DYLD_FALLBACK_LIBRARY_PATH on Darwin (OS X) and LD_LIBRARY_PATH elsewhere
if test -z "${DYLD_FALLBACK_LIBRARY_PATH}"; then
  DYLD_FALLBACK_LIBRARY_PATH="${R_LD_LIBRARY_PATH}"
else
  DYLD_FALLBACK_LIBRARY_PATH="${R_LD_LIBRARY_PATH}:${DYLD_FALLBACK_LIBRARY_PATH}"
fi
export DYLD_FALLBACK_LIBRARY_PATH

答案 1

关于SO上的rJava有很多相互矛盾的信息。我对很多这些答案的担忧是,一旦你开始对变量进行猴子攻击,你就会冒着完全无聊的Java安装的风险 - 解决方案可能比疾病更糟糕。以下是如果您遇到rJava问题,您可以做的“不伤害”事情的快速概述。JAVA_HOME

1)在终端中,运行。这是一个由 R Core 编写的脚本,它将“检测当前的 Java 设置并更新 R 中的相应配置”。请看一下此处的内部结构R CMD javareconf

2) 从源代码重新安装 rJava。.install.packages("rJava", type = "source")

3) 从命令行打开 R。加载 rJava。从命令行(方向)打开 RStudio。加载 rJava。您是否遇到相同的错误?如果没有,那就太好了 - 你越来越暖和了!你已将问题隔离到 RStudio 的问题,而不是 R 本身。

4)RStudio和rJava存在某种问题 - 实际上,你知道吗,我甚至不会试图完成这句话,因为坦率地说,它高于我的工资等级。动态库的东西。

网络上有一堆关于此的文章(这里这里这里和这里)。我最喜欢的标题是“rJava的噩梦”。

你的里程可能会有所不同,这个SO答案是我让RStudio玩得好所需要的 - 终端上的一行代码,它所做的就是创建一个符号链接。无需更改权限,无需修改环境变量 - 只是一个简单的符号链接。

如果这对您不起作用,我建议您在开始复制/粘贴大量对我有用的答案之前阅读上面链接的博客文章。


答案 2

我遇到了类似的问题,但这个解决方案对我不起作用。我最终让它工作了,但现在我不确定我改变的哪些东西解决了这个问题。以下是我所做的:

  1. 我添加了以下行到我的:.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. 我在R中设置了我的选项和环境变量:java.homeDYLD_FALLBACK_LIBRARY_PATH

    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. 我重新安装了Apple的Java 1.6版本以防万一,根据此问题线程:https://github.com/s-u/rJava/issues/37

  4. 我重新配置了R的java设置。在 Bash 中:R CMD javareconf
  5. 我以 root 身份运行 R,然后直接从 RForge 的源代码安装 rJava。在 Bash 中: .然后在 R 会话中:。sudo Rinstall.packages('rJava',,'http://www.rforge.net/')

从源代码安装对于使rJava掌握R的新Java设置至关重要。当我没有从源代码安装时,rJava可以安装,但它会继续使用JRE 1.6。您可以通过运行以下命令来检查哪些 JRE rJava 使用:

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

以 root 身份运行 R 对于从 RForge 源代码正确安装 rJava 至关重要。当我尝试以用户身份运行时,我收到以下错误消息:install.packages('rJava',,'http://www.rforge.net/')

  • 如果在命令行中运行 R,则 rJava 将无法正确安装:

    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’
    
  • 如果从 Finder 将 R 作为应用程序运行,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’
    

希望这个答案能为其他人节省一些时间!

编辑以添加:我的两位教授按照这些说明操作并遇到了以下问题:在命令行中运行R时,rJava可以工作,但在运行RStudio或默认的Mac R应用程序时无法加载。

乔·拉姆齐(Joe Ramsey)找到了解决方案。他写道:

显然,RStudio抱怨必须使用Apple用于打开应用程序的默认用户/目录。

本文介绍它:http://jeromyanglim.tumblr.com/post/34221143729/how-to-run-rstudio-from-the-command-line-on-osx

[要解决此问题]转到命令行并键入:或open -a rstudioopen -a R

编辑第二:我刚刚在其中一台学校计算机上安装了rJava,运行Ubuntu 14.04.4 LTS(64位)。我能够在以root身份运行R时安装rJava。但是,当我尝试以用户身份运行R并加载包时,我得到了一个全新的加载错误:

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

我检查了目录;rJava.so 肯定存在。事实证明,我没有正确的权限:

...$ 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

所以我改变了权限:sudo chmod -R a+rX /home/lizziesilver/R/x86_64-pc-linux-gnu-library/3.2/rJava/

然后重新配置了R的java设置:

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

现在,rJava 加载,即使以用户而不是 root 身份运行 R!

rJava:不断给出的包(配置错误)


推荐