如何让rJava在osx上使用更新版本的java? [英] How can I make rJava use the newer version of java on osx?

查看:288
本文介绍了如何让rJava在osx上使用更新版本的java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

I have made all the files as specified in the tutorial and installed the helloJavaWorld package, but once I run the helloJavaWorld() function, it complains:

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

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

So I tried to check the java version that rJava is using:

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

It looks like it uses java 1.6 (preinstalled on OSX), but I compiled the HelloJavaWorld class with java 1.8, which leads to the question: How can I make rJava use the newer version of 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

Content of /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的内容

Content of /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


推荐答案

关于SO的rJava有很多相互矛盾的信息。我对很多这些答案的关注是,一旦你开始使用 JAVA_HOME 变量进行修改,你就有可能完全破坏你的Java安装 - 解决方案可能比这种病。如果您遇到rJava问题,可以快速完成无伤害的事情。

There's a lot of conflicting info about rJava on SO. My concern with a lot of these answers was that once you start monkeying with the JAVA_HOME variables, you run the risk of borking your Java install completely -- the solution can be worse than the disease. Here's a quick rundown of 'do no harm' things you can do if you are having rJava problems.

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

1) in the terminal, run R CMD javareconf. This is a script written by R Core that will "Detect current Java setup and update the corresponding configuration in R." Take a look at the internals here.

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

2) re-install rJava from source. install.packages("rJava", type = "source").

3)从命令打开R线。加载rJava。从命令行打开 RStudio 指示)。加载rJava。你得到同样的错误吗?如果没有,那很好 - 你变暖了!您已将问题与 RStudio ,而不是R本身隔离开来。

3) Open R from the command line. Load rJava. Open RStudio from the command line (directions). Load rJava. Do you get the same error? If not, great - you're getting warmer! You've isolated your problem to an issue with RStudio, not R itself.

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

4) There's some sort of issue with RStudio and rJava that relates to - actually, you know what, I'm not even going to try to finish that sentence, because frankly it's above my pay grade. Dynamic libraries something something.

有很多写作(这里这里在这里和此处)网上有关此事。我最喜欢的标题是 rJava噩梦

There are a bunch of writeups (here, here, here, and here) on the web about this. My favorite title was "the rJava nightmare."

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

Your mileage may vary, but this SO answer was all I needed to get RStudio to play nice -- one line of code on the terminal, and all it does is creates a symbolic link. No changes to permissions, no modification of environment variables - just a simple symlink.

如果这不适合你,我建议阅读链接的博客文章在你开始复制/粘贴大量这对我有用的答案之前,你可以看到这些。

If that doesn't do it for you, I'd suggest reading the linked blog posts above before you start copy/pasting the multitude of this worked for me answers littered across SO.

这篇关于如何让rJava在osx上使用更新版本的java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆