7

有没有人设法让 rJava 0.9-3 在 OS X 10.7.4 和 Oracle Java 1.7 上工作?

R version 2.15.2 (2012-10-26) -- "Trick or Treat" Platform
x86_64-apple-darwin9.8.0/x86_64 (64-bit)

java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)

我已经使用 install.packages 函数和命令行安装 R 2.15.2 安装了 rJava。我没有收到任何错误。

$ R CMD INSTALL rJava
* installing to library ‘/Library/Frameworks/R.framework/Versions/2.15/Resources/library’
* installing *binary* package ‘rJava’ ...

* DONE (rJava)
$ 

如果我使用 Java Preferences 将 1.7 放在列表的首位,但将 1.6 保留为“on”,则 rJava 总是从 1.6 开始。

如果我使用 Java Preferences 将 1.7 放在首位并关闭 1.6,rJava 会这样做:

> library(rJava)
> .jinit()
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 in .jinit() : JNI_GetCreatedJavaVMs returned -1
> 

但是,使用 system 函数,R 本身会找到 Java 1.7。

> system("java -version")
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
> 

1.7 是否需要 /System/Library/Java/JavaConfig.plist 中的特殊条目?

Versions 目录是否需要针对 1.7 进行调整?奇怪的是 Current 转到 A,最终以 1.7 结束,而 CurrentJDK 映射到 1.6

$cd /System/Library/Frameworks/JavaVM.framework/Versions/
$ls -la
total 64
drwxr-xr-x  11 root  wheel  374 Sep 26 10:10 .
drwxr-xr-x  11 root  wheel  374 Nov 26 09:24 ..
lrwxr-xr-x   1 root  wheel   10 Sep 26 10:09 1.4 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 Sep 26 10:09 1.4.2 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 Sep 26 10:09 1.5 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 Sep 26 10:09 1.5.0 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 Sep 26 10:09 1.6 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 Sep 26 10:09 1.6.0 -> CurrentJDK
drwxr-xr-x   8 root  wheel  272 Nov 26 09:24 A
lrwxr-xr-x   1 root  wheel    1 Sep 26 10:09 Current -> A
lrwxr-xr-x   1 root  wheel   59 Sep 26 10:09 CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents

即使 R CMD javareconf 使用直接指向 1.7 文件或 /usr/bin/java 路径的路径运行,rJava 仍然无法找到或启动 Java 1.7。

1.7 需要设置 JAVA_LD_LIBRARY_PATH 变量吗?

Makeconf:JAVA = /usr/bin/java
Makeconf:JAVAC = /usr/bin/javac
Makeconf:JAVAH = /usr/bin/javah
Makeconf:JAVA_HOME = /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre
Makeconf:JAVA_LD_LIBRARY_PATH = 
Makeconf:JAVA_LIBS = -framework JavaVM
Makeconf:JAVA_CPPFLAGS = -I$(JAVA_HOME)/include

Makeconf:JAR = /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/bin/jar
Makeconf:JAVA = /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/bin/java
Makeconf:JAVAC = /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/bin/javac
Makeconf:JAVAH = /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/bin/javah
Makeconf:JAVA_HOME = /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre
Makeconf:JAVA_LD_LIBRARY_PATH = 
Makeconf:JAVA_LIBS = -framework JavaVM
Makeconf:JAVA_CPPFLAGS = -I$(JAVA_HOME)/include

谢谢你的帮助

更新1:

我在 stats-rosuda-devel 列表中询问了这个问题,并收到了 Simon Urbanek 的以下回复。我不认为 Windows 和 Linux 在需要从源代码编译 Java 1.7 方面存在相同的问题,因为我们在 Windows 和 Linux 上运行它并且不必编译 rJava。一旦我有机会尝试这个,我会报告:

您正在使用针对系统 Java(在您的系统上是 1.6)编译的二进制文件- 所以如果您删除 Java 1.6,您将破坏它。如果要使用 Java 1.7,则必须为 Java 1.7 重新配置 R 并重新编译 rJava 以使用这些设置。请注意,Oracle 的 Java 与系统 Java (/System/Library/Frameworks/JavaVM.framework) 位于完全不同的位置 (/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk),因此不可互换。

此外,Oracle Java 1.7 不是一个框架,它是一团乱麻,并不是真正为 OS X 设计的,所以你必须像在 Linux 上那样手动设置它——这非常痛苦。它还需要覆盖......所以如果你真的想进入兔子洞,这对我有用:

JAVA_LD_LIBRARY_PATH=/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/server
JAVA_LIBS='-L/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/server -ljvm'
JAVA_CPPFLAGS='-I/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/include/darwin'
R64 CMD javareconf

然后从源代码安装 rJava (R64 -> install.packages("rJava",type='source'))。请注意,它仅适用于 64 位,因此仅适用于 64 位 R。

4

2 回答 2

5

这对我有用:

情况是我的 Java 目录配置不正确。

1)第一次运行:sudo R CMD javareconf。它应该如下:

Java interpreter : /usr/bin/java
Java version     : 1.7.0_40
Java home path   : /Library/Java/JavaVirtualMachines/jdk1.7.0_40.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
llvm-gcc-4.2 -arch x86_64 -std=gnu99 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/jre/../include/darwin -I/usr/local/include    -fPIC  -mtune=core2 -g -O2  -c conftest.c -o conftest.o
llvm-gcc-4.2 -arch x86_64 -std=gnu99 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/lib -L/usr/local/lib -o conftest.so conftest.o -L/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/jre/lib/server -ljvm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation


JAVA_HOME        : /Library/Java/JavaVirtualMachines/jdk1.7.0_40.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.

2)在R中,运行:install.packages("rJava",type='source')。安装应返回 DONE(rJava)。请注意,我必须设置

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home

当我需要以 root 身份启动 R 时:sudo R。要以普通用户身份运行,请使用

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/jre

我碰巧没有将上述 /usr/bin/java* 的符号链接设置为 Java 1.7 中的相应符号链接(而是指向 1.6 版),并从那里得到了所有的挫败感。链接应该是这样的:(看起来 R 中只需要 java、javac、javah 和 jar)。

$ ls -l /usr/bin/ja*
lrwxr-xr-x  1 root  wheel  71 Oct  3 11:44 /usr/bin/jar -> /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/jar
lrwxr-xr-x  1 root  wheel  77 Oct  3 12:14 /usr/bin/jarsigner -> /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/jarsigner
lrwxr-xr-x  1 root  wheel  72 Oct  3 11:44 /usr/bin/java -> /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/java
lrwxr-xr-x  1 root  wheel  73 Oct  3 11:42 /usr/bin/javac -> /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/javac
lrwxr-xr-x  1 root  wheel  75 Oct  3 12:15 /usr/bin/javadoc -> /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/javadoc
lrwxr-xr-x  1 root  wheel  73 Oct  3 11:44 /usr/bin/javah -> /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/javah
lrwxr-xr-x  1 root  wheel  73 Oct  3 12:15 /usr/bin/javap -> /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/javap
lrwxr-xr-x  1 root  wheel  74 Oct  3 12:15 /usr/bin/javaws -> /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/javaws
于 2013-10-03T05:18:09.107 回答
1

终于,有些东西奏效了!

Simon Urbanek:您能否将这些说明放在 rJava 网站/cran 安装说明上...?这是我第一次进入这个兔子洞,我相信我不是唯一一个。

JAVA_LD_LIBRARY_PATH=/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/jre/lib/server JAVA_LIBS='-L/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/jre/lib/server -ljvm' JAVA_CPPFLAGS='-I/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/include/darwin' R CMD javareconf
Java interpreter : /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/jre/bin/java
Java version     : 1.8.0_77
Java home path   : /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home
Java compiler    : /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/bin/javac
Java headers gen.: /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/bin/javah
Java archive tool: /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/bin/jar
System Java on OS X

trying to compile and link a JNI program
detected JNI cpp flags    : -I/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/include/darwin
detected JNI linker flags : -L/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/jre/lib/server -ljvm
clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/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_77.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_77.jdk/Contents/Home
Java library path: /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/jre/lib/server
JNI cpp flags    : -I/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/include/darwin
JNI linker flags : -L/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/jre/lib/server -ljvm
Updating Java configuration in /Library/Frameworks/R.framework/Resources
override rw-r--r--  root/admin for /Library/Frameworks/R.framework/Resources/etc/Makeconf? (y/n [n]) y
override rw-r--r--  root/admin for /Library/Frameworks/R.framework/Resources/etc/ldpaths? (y/n [n]) y
Done.

R CMD INSTALL rJava_0.9-9.tar.gz
于 2016-08-25T22:11:50.543 回答