Using rJava with RStudio Workbench / RStudio Server Pro, Launcher and Kubernetes


If you’re using RStudio Workbench (previously RStudio Server Pro) with Launcher and Kubernetes, you’re probably also using our r-session-complete docker images, or perhaps the Dockerfiles on which the images are based as the basis for your own containers.


These container resources are provided as-is and free of charge, as a convenience to the community. One thing that we do not currently include in the container howerver, is a Java installation. This is ommitted as it's increasingly rarely used and makes the container images larger. Unfortunately, this means that the rJava package does not work inside the container out-of-the-box and in this article we will outline the steps required to get rJava working. 


The error you see when you try to load rJava:

Error: package or namespace load failed for 'rJava':
  .onlaod failed in loadNamespace() for 'rJava', details:
    call: dyn.load(file, DLLpath = DLLpath, ...)
    error: unable to load shared object '/opt/R/4.0.2/lib/R/library/rJava/libs/': cannot open shared object file: No such file or directory


The resolution described here requires you to build and host your own container image. The specifics of how to do this will vary from environment to environment and are outside of the RStudio product suite. For additional information on these process, please consult the Docker documentation as well as the documentation for your chosen container registry.


You can find a complete example of the Dockerfile we use on GitHub


We won’t walk through the complete example here since we’re focused on the rJava fixes. Instead, we’ll highlight the important sections and provide the additional information required to fix the issue so that you can apply them as appropriate to your own Dockerfile. Many of the lines below will likely already exist in your Dockerfile, either exactly as they’re presented here, or perhaps combined with other commands.


For the rest of these steps to work, we need to set the R version. In our Dockerfile above we set this with an environment variable, so we can use the same value in the rest of the file.




Then we need to install the Java JDK. It’s important to install the JDK (Java Development Kit) rather than the JRE (Java Runtime Environment) as the JDK contains additional development tools required by rJava.


RUN apt-get install -y openjdk-8-jdk


Install the rJava package itself:


RUN /opt/R/${R_VERSION}/bin/R -e 'install.packages("rJava", repos="")'


Configure R to use the newly installed Java version:


RUN /opt/R/${R_VERSION}/bin/R CMD javareconf JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/


Now for the fix itself. We need to force the installed R version to use it's own ldpaths startup script when it starts inside the container.


RUN mkdir -p /etc/rstudio && printf "Path: /opt/R/${R_VERSION}\nScript: /opt/R/${R_VERSION}/lib/R/etc/ldpaths" > /etc/rstudio/r-versions


With that added to the Dockerfile, when the new image is built and in use, you’ll be able to launch sessions that properly allow rJava to run.


This last step is necessary to ensure that R starts with the appropriate LD_LIBRARY_PATH variable set. Without it, rJava won’t work in the containerised session, even if you manually set the correct LD_LIBRARY_PATH environment variable manually in R yourself.