title: Rstudio Knit button fail to load rJava
body: <p>The knit button of Rstudio is failing to knit rmarkdown (Rmd) files which includes the rJava package. However, if I use <code>rmarkdown::render()</code>, the same Rmd files render into html without problems. </p>
  
  <p>I can reproduce the error with the following code:</p>
  
  <p>content of test.Rmd:</p>
  
  <pre><code>---
  title: "test"
  output: html_document
  ---
  
  ```{r}
  library(rJava)
  ```
  </code></pre>
  
  <p>Clicking the Knit button button would return:</p>
  
  <pre><code>processing file: test.Rmd
  
  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.3/Resources/library/rJava/libs/rJava.so':
    dlopen(/Library/Frameworks/R.framework/Versions/3.3/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
    Referenced from: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rJava/libs/rJava.so
    Reason: image not found
  Quitting from lines 7-8 (test.Rmd) 
  Error: package or namespace load failed for 'rJava'
  Execution halted
  </code></pre>
  
  <p>And <code>rmarkdown::render("test.Rmd")</code> works fine. Also, running <code>library(rJava)</code> normally (not knitting) works fine too (within RStudio).</p>
  
  <p>Using RStudio version 1.0.136, knitr 1.15.1, rmarkdown 1.3, rJava 0.9-8, R 3.3.2 in macOS Sierra 10.12.</p>
  
  <p><strong>Update:</strong> Adding output of <code>Sys.getenv()</code> as requested by @r2evans. There is a difference in the value of DYLD_FALLBACK_LIBRARY_PATH, but I don't know if this is significant. I replace some private info with XXX.</p>
  
  <p>In the html file resulting from Knit button:</p>
  
  <pre><code>## __CF_USER_TEXT_ENCODING
  ##                       0x1F5:0x0:0x52
  ## Apple_PubSub_Socket_Render
  ##                       /private/tmp/com.apple.launchd.HvKScioyGU/Render
  ## DISPLAY               :0
  ## DYLD_FALLBACK_LIBRARY_PATH
  ##                       /Library/Frameworks/R.framework/Resources/lib
  ## EDITOR                vi
  ## GIT_ASKPASS           rpostback-askpass
  ## HOME                  XXX
  ## LANG                  en_CA.UTF-8
  ## LC_CTYPE              en_CA.UTF-8
  ## LN_S                  ln -s
  ## LOGNAME               XXX
  ## MAKE                  make
  ## NOT_CRAN              true
  ## PAGER                 /usr/bin/less
  ## PATH                  /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/TeX/texbin
  ## PWD                   XXX
  ## R_ARCH                
  ## R_BROWSER             /usr/bin/open
  ## R_BZIPCMD             /usr/bin/bzip2
  ## R_DOC_DIR             /Library/Frameworks/R.framework/Resources/doc
  ## R_GZIPCMD             /usr/bin/gzip
  ## R_HOME                /Library/Frameworks/R.framework/Resources
  ## R_INCLUDE_DIR         /Library/Frameworks/R.framework/Resources/include
  ## R_LIBS                /Library/Frameworks/R.framework/Versions/3.3/Resources/library
  ## R_LIBS_SITE           
  ## R_LIBS_USER           ~/Library/R/3.3/library
  ## R_PAPERSIZE           a4
  ## R_PAPERSIZE_USER      a4
  ## R_PDFVIEWER           /usr/bin/open
  ## R_PLATFORM            x86_64-apple-darwin13.4.0
  ## R_PRINTCMD            lpr
  ## R_QPDF                /Library/Frameworks/R.framework/Resources/bin/qpdf
  ## R_RD4PDF              times,inconsolata,hyper
  ## R_SESSION_TMPDIR      /var/folders/15/vdvn4fvx0zn0gc93_5krp6h00000gn/T//RtmpiYlwpU
  ## R_SHARE_DIR           /Library/Frameworks/R.framework/Resources/share
  ## R_SYSTEM_ABI          osx,gcc,gxx,gfortran,?
  ## R_TEXI2DVICMD         /usr/local/bin/texi2dvi
  ## R_UNZIPCMD            /usr/bin/unzip
  ## R_ZIPCMD              /usr/bin/zip
  ## RMARKDOWN_MATHJAX_PATH
  ##                       /Applications/RStudio.app/Contents/Resources/resources/mathjax-26
  ## RMARKDOWN_PREVIEW_DIR
  ##                       /var/folders/15/vdvn4fvx0zn0gc93_5krp6h00000gn/T//RtmpLe3D9z
  ## RS_RPOSTBACK_PATH     /Applications/RStudio.app/Contents/MacOS/rpostback
  ## RS_SHARED_SECRET      XXX
  ## RSTUDIO               1
  ## RSTUDIO_PANDOC        /Applications/RStudio.app/Contents/MacOS/pandoc
  ## RSTUDIO_SESSION_PORT
  ##                       XXX
  ## RSTUDIO_USER_IDENTITY
  ##                       XXX
  ## RSTUDIO_WINUTILS      bin/winutils
  ## SED                   /usr/bin/sed
  ## SHELL                 /bin/bash
  ## SHLVL                 0
  ## SSH_AUTH_SOCK         XXX
  ## TAR                   /usr/bin/tar
  ## TMPDIR                /var/folders/15/vdvn4fvx0zn0gc93_5krp6h00000gn/T/
  ## USER                  XXX
  ## XPC_FLAGS             0x0
  ## XPC_SERVICE_NAME      0
  </code></pre>
  
  <p>In the file resulting from <code>rmarkdown::render()</code>:</p>
  
  <pre><code>## __CF_USER_TEXT_ENCODING
  ##                       0x1F5:0x0:0x52
  ## Apple_PubSub_Socket_Render
  ##                       /private/tmp/com.apple.launchd.HvKScioyGU/Render
  ## DISPLAY               :0
  ## DYLD_FALLBACK_LIBRARY_PATH
  ##                       /Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/lib:/Users/Jayme/lib:/usr/local/lib:/usr/lib::::::::
  ## EDITOR                vi
  ## GIT_ASKPASS           rpostback-askpass
  ## HOME                  XXX
  ## LANG                  en_CA.UTF-8
  ## LC_CTYPE              en_CA.UTF-8
  ## LN_S                  ln -s
  ## LOGNAME               XXX
  ## MAKE                  make
  ## PAGER                 /usr/bin/less
  ## PATH                  /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/TeX/texbin
  ## R_BROWSER             /usr/bin/open
  ## R_BZIPCMD             /usr/bin/bzip2
  ## R_DOC_DIR             /Library/Frameworks/R.framework/Resources/doc
  ## R_GZIPCMD             /usr/bin/gzip
  ## R_HOME                /Library/Frameworks/R.framework/Resources
  ## R_INCLUDE_DIR         /Library/Frameworks/R.framework/Resources/include
  ## R_LIBS_SITE           
  ## R_LIBS_USER           ~/Library/R/3.3/library
  ## R_PAPERSIZE           a4
  ## R_PDFVIEWER           /usr/bin/open
  ## R_PLATFORM            x86_64-apple-darwin13.4.0
  ## R_PRINTCMD            lpr
  ## R_QPDF                /Library/Frameworks/R.framework/Resources/bin/qpdf
  ## R_RD4PDF              times,inconsolata,hyper
  ## R_SESSION_TMPDIR      /var/folders/15/vdvn4fvx0zn0gc93_5krp6h00000gn/T//RtmpLe3D9z
  ## R_SHARE_DIR           /Library/Frameworks/R.framework/Resources/share
  ## R_SYSTEM_ABI          osx,gcc,gxx,gfortran,?
  ## R_TEXI2DVICMD         /usr/local/bin/texi2dvi
  ## R_UNZIPCMD            /usr/bin/unzip
  ## R_ZIPCMD              /usr/bin/zip
  ## RMARKDOWN_MATHJAX_PATH
  ##                       /Applications/RStudio.app/Contents/Resources/resources/mathjax-26
  ## RS_RPOSTBACK_PATH     /Applications/RStudio.app/Contents/MacOS/rpostback
  ## RS_SHARED_SECRET      XXX
  ## RSTUDIO               1
  ## RSTUDIO_PANDOC        /Applications/RStudio.app/Contents/MacOS/pandoc
  ## RSTUDIO_SESSION_PORT
  ##                       XXX
  ## RSTUDIO_USER_IDENTITY
  ##                       XXX
  ## RSTUDIO_WINUTILS      bin/winutils
  ## SED                   /usr/bin/sed
  ## SHELL                 /bin/bash
  ## SSH_AUTH_SOCK         XXX
  ## TAR                   /usr/bin/tar
  ## TMPDIR                /var/folders/15/vdvn4fvx0zn0gc93_5krp6h00000gn/T/
  ## USER                  XXX
  ## XPC_FLAGS             0x0
  ## XPC_SERVICE_NAME      0
  </code></pre>
  
  <p><strong>Update2</strong> Following comments from @r2evans</p>
  
  <p>Including the DYLD_FALLBACK_LIBRARY_PATH from <code>render()</code> in the <code>.Rmd</code> changes the result of <code>Sys.getenv()</code> as expected, but the behaviour of error with knit button (same error message as above) and no error in <code>render()</code> persists. The new <code>.Rmd</code> content is:</p>
  
  <pre><code>---
  title: "test2"
  output: html_document
  ---
  
  ```{r}
  Sys.setenv(DYLD_FALLBACK_LIBRARY_PATH="/Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/lib:/Users/Jayme/lib:/usr/local/lib:/usr/lib::::::::")
  library(rJava)
  ```
  </code></pre>
  
  <p>Interestingly, using <code>render()</code> in this <code>test2.Rmd</code> just above, and including <code>Sys.getenv()</code> now outputs two new variables, not present before and not present using the knit button:</p>
  
  <pre><code>## DYLD_LIBRARY_PATH     /Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/server
  ## LD_LIBRARY_PATH       :@JAVA_LD@
  </code></pre>
  
  <p>I don't if it makes sense or not, but I tried a new <code>.Rmd</code> (below), which still gives the same error</p>
  
  <pre><code>---
  title: "test3"
  output: html_document
  ---
  
  ```{r}
  Sys.setenv(DYLD_FALLBACK_LIBRARY_PATH="/Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/lib:/Users/Jayme/lib:/usr/local/lib:/usr/lib::::::::")
  Sys.setenv(DYLD_LIBRARY_PATH="/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/server")
  Sys.setenv(LD_LIBRARY_PATH= ":@JAVA_LD@")
  library(rJava)
  ```
  </code></pre>