简体   繁体   English

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

[英]How can I make rJava use the newer version of java on osx?

I am following this tutorial on rJava: http://cran.r-project.org/web/packages/helloJavaWorld/vignettes/helloJavaWorld.pdf 我在rJava上关注本教程: http ://cran.r-project.org/web/packages/helloJavaWorld/vignettes/helloJavaWorld.pdf

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包,但是一旦我运行了helloJavaWorld()函数,就会抱怨:

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

So I tried to check the java version that rJava is using: 所以我试着检查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="

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? 它看起来像是使用java 1.6(预装在OSX上),但我使用java 1.8编译了HelloJavaWorld类,这导致了一个问题:如何让rJava使用更新版本的java?

Output of R CMD javareconf : 输出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.

Content of /Library/Frameworks/R.framework/Versions/3.1/Resources/etc/javaconf /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~}

Content of /Library/Frameworks/R.framework/Versions/3.1/Resources/etc/ldpaths /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

There's a lot of conflicting info about rJava on SO. 在SO上有很多关于rJava的相互矛盾的信息。 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. 我对很多这些答案的关注是,一旦你开始使用JAVA_HOME变量进行修改,就会冒着完全堵塞Java安装的风险 - 解决方案可能比疾病更糟糕。 Here's a quick rundown of 'do no harm' things you can do if you are having rJava problems. 如果您遇到rJava问题,可以快速了解“不伤害”的事情。

1) in the terminal, run R CMD javareconf . 1)在终端中,运行R CMD javareconf This is a script written by R Core that will "Detect current Java setup and update the corresponding configuration in R." 这是由R Core编写的脚本,它将“检测当前的Java设置并更新R中的相应配置”。 Take a look at the internals here . 看看这里的内部。

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

3) Open R from the command line. 3)从命令行打开R. Load rJava. 加载rJava。 Open RStudio from the command line ( directions ). 从命令行( 方向 )打开RStudio Load rJava. 加载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. 您已将问题与RStudio的问题隔离,而不是R本身。

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. 4)与RStudio和rJava存在某种问题 - 实际上,你知道什么,我甚至都不会试图完成那句话,因为坦率地说它高于我的工资等级。 Dynamic libraries something something. 动态库有些东西。

There are a bunch of writeups ( here , here , here , and here ) on the web about this. 网上有很多关于此的文章( 这里这里这里这里 )。 My favorite title was " the rJava nightmare ." 我最喜欢的标题是“ rJava噩梦”

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. 你的里程可能会有所不同,这个 SO答案就是我需要让RStudio发挥得很好 - 终端上的一行代码,它所做的就是创建一个符号链接。 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. 如果这不适合你,我建议你先阅读上面链接的博客文章,然后再开始复制/粘贴大量的这些文章

I had a similar problem, but this solution didn't work for me. 我有类似的问题,但这个解决方案对我不起作用。 I eventually got it working, but now I'm not sure which of the things I changed solved the problem. 我最终得到了它的工作,但现在我不确定我改变了哪些东西解决了这个问题。 Here's what I did: 这是我做的:

  1. I added the following lines to my .bash_profile : 我在.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. I set my java.home option and my DYLD_FALLBACK_LIBRARY_PATH environmental variable in R: 我在R中设置了我的java.home选项和我的DYLD_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. I re-installed Apple's version of Java 1.6 just in case, as per this issue thread: https://github.com/su/rJava/issues/37 我重新安装了Apple的Java 1.6版本以防万一,根据这个问题线程: https//github.com/su/rJava/issues/37

  4. I reconfigured R's java settings. 我重新配置了R的java设置。 In Bash: R CMD javareconf 在Bash中: R CMD javareconf
  5. I ran R as root, and then installed rJava from source, directly from RForge. 我以root身份运行R,然后从源代码直接从RForge安装rJava。 In Bash: sudo R . 在Bash: sudo R Then in the R session: install.packages('rJava',,'http://www.rforge.net/') . 然后在R会话中: install.packages('rJava',,'http://www.rforge.net/')

Installing from source was crucial to making rJava pick up on R's new Java settings. 从源代码安装对于让rJava接受R的新Java设置至关重要。 When I didn't install from source, rJava would install ok, but it would continue to use JRE 1.6. 当我没有从源代码安装时,rJava会安装好,但它会继续使用JRE 1.6。 You can check which JRE rJava uses by running: 您可以通过运行来检查哪个JRE rJava使用:

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

Running R as root was crucial to making rJava install correctly from the RForge source. 以root身份运行R对于从RForge源正确安装rJava至关重要。 When I tried to run install.packages('rJava',,'http://www.rforge.net/') as User, I got the following error messages: 当我尝试以用户身份运行install.packages('rJava',,'http://www.rforge.net/') ,收到以下错误消息:

  • If running R in the command line, rJava would not install correctly: 如果在命令行中运行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' 
  • If running R as an application from Finder, rJava would install, but not load: 如果从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' 

Hopefully this answer will save someone else some time! 希望这个答案可以节省别人一些时间!

Edited to add: Two of my professors followed these instructions and ran into the following issue: rJava would work when running R in the command line, but would fail to load when running RStudio or the default Mac R app. 编辑添加:我的两位教授遵循这些说明并遇到了以下问题:rJava在命令行中运行R时会起作用,但在运行RStudio或默认的Mac R应用程序时无法加载。

Joe Ramsey figured out the solution. 乔拉姆齐想出了解决方案。 He writes: 他写:

Apparently RStudio grumbles about having to use the default user/directory that Apple uses to open applications. 显然,RStudio抱怨必须使用Apple用来打开应用程序的默认用户/目录。

This article describes it: http://jeromyanglim.tumblr.com/post/34221143729/how-to-run-rstudio-from-the-command-line-on-osx 本文描述了它: http//jeromyanglim.tumblr.com/post/34221143729/how-to-run-rstudio-from-the-command-line-on-osx

[To fix the issue] You go to the command line and type: open -a rstudio or open -a R [解决此问题]您转到命令行并键入: open -a rstudioopen -a R

Edit number two : I just installed rJava on one of the school computers, running Ubuntu 14.04.4 LTS (64-bit). 编辑第二 :我刚刚在其中一台学校计算机上安装了rJava,运行Ubuntu 14.04.4 LTS(64位)。 I was able to install rJava when running R as root. 我以root身份运行R时能够安装rJava。 However, when I then tried to run R as user and load the package, I got a brand new loading error: 但是,当我尝试以用户身份运行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’

I checked the directory; 我检查了目录; rJava.so definitely existed. rJava.so绝对存在。 It turned out that I didn't have the right permissions for it: 原来,我没有正确的权限:

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

So I changed the permissions: sudo chmod -R a+rX /home/lizziesilver/R/x86_64-pc-linux-gnu-library/3.2/rJava/ 所以我更改了权限: sudo chmod -R a+rX /home/lizziesilver/R/x86_64-pc-linux-gnu-library/3.2/rJava/

Then reconfigured R's java settings: 然后重新配置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

Now rJava loads, even when running R as user instead of root! 现在rJava加载,即使以用户而不是root运行R!

rJava: the package that keeps on giving (config errors) rJava:不断给出的包(配置错误)

I solved the problem by installing from the latest source package on RForge: http://www.rforge.net/rJava/files/ 我通过在RForge上安装最新的源代码包解决了这个问题: http ://www.rforge.net/rJava/files/

cd /tmp
wget http://www.rforge.net/rJava/snapshot/rJava_0.9-7.tar.gz
R CMD INSTALL rJava_0.9-7.tar.gz

In R: 在R:

> library(helloJavaWorld)
Loading required package: rJava
> helloJavaWorld()
[1] "Hello from java!"

Here is a solution that worked pretty good for me: 这是一个对我有用的解决方案:

  1. In the terminal run: R CMD javareconf 在终端运行中:R CMD javareconf

  2. Get the JAVA_HOME path and the Java library path from the output Example 从输出示例中获取JAVA_HOME路径和Java库路径

JAVA_HOME : /Library/Java/JavaVirtualMachines/jdk1.8.0_65.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 JAVA_HOME:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre Java库路径:$(JAVA_HOME)/ lib / server JNI cpp标志:-I $(JAVA_HOME)/../ include -I $(JAVA_HOME)/../ include / darwin JNI链接器标志:-L $(JAVA_HOME)/ lib / server -ljvm在/Library/Frameworks/R.framework/Resources中更新Java配置

  1. Then, make sure you don't have an open instance of RStudio. 然后,确保您没有RStudio的打开实例。 We'll create an alias that will open RStudio with the correct path to your latest java version. 我们将创建一个别名,用于打开RStudio并使用最新java版本的正确路径。 Run this in your commend line 在您的推荐行中运行此命令

alias rstudio="DYLD_FALLBACK_LIBRARY_PATH=/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/server open -a RStudio" 别名rstudio =“DYLD_FALLBACK_LIBRARY_PATH = / Library / Java / JavaVirtualMachines / jdk1.8.0_65.jdk / Contents / Home / jre / lib / server open -a RStudio”

  1. Type rstudio in your terminal 在终端中键入rstudio

  2. Done! 完成! Rstudio will open pointing to the right java version Rstudio将打开指向正确的java版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使rJava或R使用不同的(先前的)Java版本 - Make rJava or R to use a different (previous) Java version 我可以在旧版本的Elasticsearch上使用新版本的Java高级别Rest客户端吗? - Can I use newer version of java high level rest client on an older version of elasticsearch? 如何让 Gradle 编译使用较新 Java 版本的源库? - How do I make Gradle compile a source libary that uses newer java version? 如何在OSX上使Eclipse使用Java 1.6? - How can I get Eclipse on OSX to use Java 1.6? 如何在OSX上的Java 7中使用32位本机库 - How can I use a 32 bit native library in Java 7 on OSX 我可以用较新的JDK用ant编译Java较旧版本的Java,以便在编译使用较新API的代码时生成输出吗? - Can i compile java for an older Java version with ant with a newer JDK so it generates output when it compiles code that uses the newer API? 我如何让hadoop使用与`javac`相同的Java Runtime版本? - How can I make hadoop use the same Java Runtime version as `javac`? 我可以在战争中捆绑更新版本的Nashorn吗? - Can I bundle a newer version of Nashorn in a war? 我无法重新安装Java:弹出新版本的Java发现消息 - I can't reinstalling java: newer version of java found message pops up 如何在OSX上安装Java SDK的早期版本 - How do I install an earlier version of Java SDK on OSX
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM