title: rJava load error in RStudio/R after "upgrading" to OSX Yosemite
body: <p>I recently "upgraded" from OSX Mountain Lion to Yosemite and from R 3.1.3 to 3.2. Immediately after the upgrade, when I opened R or RStudio I got a pop-up message saying that I needed to install Java 6. In addition, loading <code>rJava</code> or any package that depends on rJava (e.g., <code>xlsx</code>) caused RStudio to crash (R also crashed when I tried this by opening <code>R.app</code> directly). </p>
  
  <p>After trying a few fixes found on Stack Overflow and elsewhere (more details below), I am at a point where loading <code>rJava</code> or any package that depends on <code>rJava</code> no longer causes R to crash, but results in the following error:</p>
  
  <pre><code>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’
  </code></pre>
  
  <p>However, if I invoke R from the command line and then load <code>rJava</code> or any package that depends on <code>rJava</code>, it seems to work (or at least I don't get any error messages).</p>
  
  <p>I've tried a number of different attempted fixes, some of them a few times, and can't quite remember exactly what I did in what order (didn't realize this would be such a morass and wasn't really keeping track), but here's the gist of it:</p>
  
  <ul>
  <li><p>Added the following to my <code>.bash_profile</code> (per <a href="https://stackoverflow.com/a/30068890/496488">this SO answer</a>):</p>
  
  <blockquote>
    <p>export JAVA_HOME="/usr/libexec/java_home -v 1.8"<br>
    export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/server</p>
  </blockquote></li>
  <li><p>Reconfigured java from the command line as follows:</p>
  
  <blockquote>
    <p>sudo R CMD javareconf -n  </p>
  </blockquote></li>
  <li><p>Checked <code>options("java.home")</code> and discovered this was set to  <code>NULL</code>. I tried setting it to the following (per <a href="https://stackoverflow.com/questions/28133360/rjava-is-not-picking-up-the-correct-java-version">this SO question</a>):</p>
  
  <blockquote>
    <p>options("java.home"="/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre")</p>
  </blockquote></li>
  <li><p>Installed the latest Java Development Kit and reinstalled <code>rJava</code> from source (can't remember where I found that one).</p></li>
  </ul>
  
  <p>At some point while trying all of these, I was able to load <code>rJava</code> without crashing R, but instead got the error message posted above. In addition, when I quit RStudio, it would seem to close normally, but then an "RStudio quit unexpectedly" message would pop up, indicating that the program had crashed while trying to close. </p>
  
  <p>I finally decided to install <a href="https://support.apple.com/kb/DL1572?locale=en_US" rel="noreferrer">Java for OS X 2014-001</a> (Java 6), as I seemed to be running out of options. Now, when I opened R or RStudio the "This software needs Java 6" pop-up message no longer appeared. However, I was still getting the <code>.onLoad failed in loadNamespace() for 'rJava'</code> error message posted above. </p>
  
  <p>In reviewing some of the posts I'd already looked at, I noticed <a href="https://stackoverflow.com/a/30581550/496488">another SO answer</a> that I'd missed before, which recommended opening RStudio with the following command line code that gives RStudio the correct path to java:</p>
  
  <blockquote>
    <p>LD_LIBRARY_PATH=$(/usr/libexec/java_home)/jre/lib/server: open -a RStudio</p>
  </blockquote>
  
  <p>That opened an RStudio window and I was also able to load <code>rJava</code> and packages that depend on it without getting an error.</p>
  
  <p>Lastly, I tried running R from the command line (which I hadn't done before). It turns out that on the command line, loading <code>rJava</code> or any package that depends on <code>rJava</code> works and does not throw any errors.</p>
  
  <p>So, I can now get <code>rJava</code> to work if I open RStudio from the command line with the code that gives RStudio the java path (as noted above). However, I'd like to find a way to fix the underlying problem, whatever it may be, so that RStudio can be opened in the usual Mac way, without needing a command line kludge. I'm also concerned that having an old version of Java installed could cause problems down the road. </p>
  
  <p>Does anyone have any ideas about how to diagnose and solve this issue?</p>