6

This question is related to this other question.

I am trying to use RNetLogo with R and get the following error.

nl.path <- "/Applications/NetLogo 5.1.0"
NLStart(nl.path) 
Error in .jnew("nlcon/Preprocess") : 
  java.lang.UnsupportedClassVersionError: nlcon/Preprocess : Unsupported major.minor version 51.0

From what I understood in this other question, the problem is that R is using an old version of Java which is incompatible with RNetLogo.

I installed Java 8.0 hoping to solve the problem but my understanding is that, despite Java 8.0 being installed on my computer (Mac OS Maverick), R does not pick it up and keep trying to use old versions of Java.

So my question is : How can I get R to use Java 8.0 instead of any older version?

In the terminal console, I get

java -version :
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

Thanks in advance for your help,

Community
  • 1
  • 1
  • 1
    What's your PATH? And what does this report (from Terminal console): `java -version` – IRTFM Nov 04 '14 at 21:48
  • @BondedDust : I added the result of the "java -version" query to my question. I am not sure which path you have in mind. Do you mean the path to JRE? – Martin Van der Linden Nov 04 '14 at 22:46
  • 1
    You are currently running version 7. There should be a Java item in the System Preferences. See whether it 'sees' the newer version. – IRTFM Nov 04 '14 at 22:54
  • That's weird, I checked the Java item in the System Preferences before (and did it again at the moment) and under the "Update" tab it says "Your system has the recommended version of Java. {linebreak} Java 8 Update 25". Is this normal? (nothwistanding the fact that according to the linked question, if I am indeed using Java 7, things should work just fine). – Martin Van der Linden Nov 04 '14 at 23:02
  • 1
    I'm no longer sure that the report from > java -version will match the version reported by the System Preferences tool. – IRTFM Nov 04 '14 at 23:17
  • Ok, thanks for trying :) If you have any other idea, feel free to share them. – Martin Van der Linden Nov 04 '14 at 23:17

8 Answers8

3

Seems like on Mac OS X you can have multiple Java at a one time.

Use below command on terminal to check how many JDK version you have.

/usr/libexec/java_home -V

You can follow below instruction to have correct Java Path setup:

How To Set $JAVA_HOME Environment Variable On Mac OS X

In nutshell do:

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

veysiertekin
  • 1,731
  • 2
  • 15
  • 40
pinakin
  • 422
  • 4
  • 16
3

Unfortunately, none of these seem to help on a Mac. Windows and Linux solutions are not relevant because the files are in different places.

If you just update to Java 1.8 (Java 8 for Oracle) in the standard way as prompted by the Java preferences pain, you just get the Java Runtime Environment (JRE). If you run...

/usr/libexec/java_home -V

...it still shows only java 1.6, and...

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

...throws an error saying it can't find a version 1.8.

To get the Mac to even recognize a newer version of Java, it seems you must install JDK v.8 (not JRE). At that point, you can get the Mac to recognize that a new java virtual machine is available, and you can do the export command successfully. (Note that the new 1.8 JVM is in a DIFFERENT place--/Library/Java instead of /System/Library/Java for 1.6.). BUT, this still does no good for R.

I've tried putting the export JAVA_HOME... command into my .profile and my .bash_profile. Then sourcing both. Works fine, but has no effect on R AFAICT. I've launched R via the standard Mac R GUI, from RStudio, and from the terminal and it is only recognizing Java 1.6. So RNetLogo still does not work.

I will try to find RNetLogo 1.0-0 in the archive and test that. If it works, I suggest that 1.0-1 be rolled back until this Java problem is solved.

2

I use this line on windows :

options(java.home="C:/Program Files/Java/jre7/")

You probably have to change the 7 for an 8 and find the proper path on the mac.

Romain
  • 839
  • 10
  • 24
1

This supposedly works (originally from this blog entry):

1) Download and install Apple’s Java version 1.6.

2) Reconfigure your Java installation by using sudo R CMD javareconf -n.

3) Reinstall rJava from source with: install.packages('rJava', type='source').

Please acknowledge Will Lowe at conjugateprior.org for the original post and solution.

Kenji
  • 571
  • 4
  • 20
1

EDIT: I don't know if anyone is still struggling with this, but with rJava 0.9-9, the 'partial fix' below no longer works. What does work, and completely, is the final solution offered here: https://github.com/s-u/rJava/issues/86

Copying from there, many thanks to Gregory R. Warnes:

  • uninstall existing rJava versions by running the following in the Terminal:

    Rscript -e 'remove.packages("rJava")'
    sudo Rscript -e 'remove.packages("rJava")'
    
  • add the following to /Users/<userid>/.bashrc:

    export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)/jre'
    

(e.g., type > vim /Users/<userid>/.bashrc in the Terminal, then 'i', add the line above, then ':wq' to save and quit)

  • close and re-start all Terminal, R and RStudio windows

  • type the following in the Terminal window:

    sudo ln -sf $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
    
  • in a new R session, re-install rJava from source:

    install.packages("rJava", repos="http://rforge.net", type="source")
    

OLD 'PARTIAL FIX' BELOW:

Okay. I have been working on this problem all morning, and I have a partial fix.

I tried the solution suggested by Guilherme Kenji Chihaya above, but even after sudo R CMD javareconf -n and install.packages('rJava', type='source'), R insists on using Java 1.6 (and is happy to do so).

HOWEVER, R studio throws an error after re-installing 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’

Googling this lead me to this post: http://andrewgoldstone.com/blog/2015/02/03/rjava/, with a working solution. Set the following in the Terminal:

alias r="DYLD_FALLBACK_LIBRARY_PATH=/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/server/: open -a r"

And start R from the Terminal. Then, magically, in R:

> library(rJava)
> .jinit()
> .jcall("java/lang/System", "S", "getProperty", "java.runtime.version")
[1] "1.8.0_11-b12"

However, this only works when starting R from the Terminal. I haven't been able to get R to automatically recognise the right "DYLD_FALLBACK_LIBRARY_PATH" in any way.

elskevdv
  • 91
  • 5
1

Try linking libjvm.dylib to /usr/lib:

sudo ln -f -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/lib

-f flag is added to force overwriting existing file/link

Alex Popov
  • 3,726
  • 1
  • 17
  • 20
0

In Ubuntu there is a command alternatives that i use it for this purpose.

alternatives --install /usr/bin/java java /usr/java/jdk1.8*/jre/bin/java 200000
alternatives --install /usr/bin/javaws javaws /usr/java/jdk1.8*/jre/bin/javaws 200000
alternatives --install /usr/bin/javac javac /usr/java/jdk1.8*/bin/javac 200000
alternatives --install /usr/bin/jar jar /usr/java/jdk1.8*/bin/jar 200000

After alternatives install use the following command to change your version. alternatives --config java and then select your newer java.

If this is not available you should first find out what where is your new java actually. locate *jdk1.8* Then find out which java you run as binary. which java this will returns the path of binary. this is the old java binary, So remove it and link new java binary in the same place. For example : ln -s /path/to/java1.8*/bin/java /usr/bin/java

In addtion you need to update your CLASS_PATH environment variable that is necessary for the VM.

for example: export CLASS_PATH=/usr/java/jdk1.8*/jre/lib and you can add this line in your bashrc file to register this configuration.

muradin
  • 1,249
  • 2
  • 14
  • 34
0

In Debian-based installations R uses /etc/R/Makeconf settings for building libraries. One of the setting there is JAVA_HOME. Try setting the correct path there and reinstall the package.

pwes
  • 2,040
  • 21
  • 30