Force a .travis.yml for a package that depends on rJava?

I (and some colleagues) have been working on a package that depends on rJava. The program we connect to needs Java 11+ to run, and we’re running into some problems while trying to configure the .travis.yml file.

language: r
warnings_are_errors: true
sudo: required
cache: packages

apt_packages:
  - r-cran-rjava

I get error:

I get the following error message:

Failed with error:  ‘.onLoad failed in loadNamespace() for 'rJava', details:

  call: dyn.load(file, DLLpath = DLLpath, ...)

  error: unable to load shared object '/home/travis/R/Library/rJava/libs/rJava.so':

  libjvm.so: cannot open shared object file: No such file or directory’

then I tweak the yml file a bit:

language: r
warnings_are_errors: true
sudo: required
cache: packages

apt_packages:
  - default-jdk

before_install:
  - export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
  - export PATH=$PATH:$JAVA_HOME/bin
  - export LD_LIBRARY_PATH=/usr/lib/jvm/java-11-openjdk-amd64/lib
  - sudo R CMD javareconf

error I get then:


The command "sudo R CMD javareconf" failed and exited with 1 during .

This is because R is in the user’s PATH, but sudo ignores it and only looks in a few system locations now. This can be solved easily by resolving the full PATH at the user level, you could also just use a bash script for the sake of time I’ll show you my fix:

  - sudo $(which R) CMD javareconf

This will insert the full PATH to R into the sudo command.

Note that the javareconf script seems smart enough to detect Java in the standard locations, (stdlibs) so it isn’t necessary to export JAVA_HOME , PATH or LD_LIBRARY_PATH before calling it, as Java already knows.

After some ‘problem solving’ I ended up with the following .travis.yml file:

---
language: r
cache: packages
warnings_are_errors: true
sudo: required

apt_packages:
  - r-cran-rjava

before_install:
  - sudo $(which R) CMD javareconf

I hope this helps, let me know if this works/doesn’t work and we can figure it out.