Search code examples
rtravis-cirjava

How to configure 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.

I have tried to follow the solutions of this answer (while adapting them to Java 11) but none seem to work. First I tried:

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

apt_packages:
  - r-cran-rjava

But 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 tried following the second approach:

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

To what I'm met with this error message:

sudo: R: command not found

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

I have tried not using the sudo, but then I get that I don't have the required permissions.

I feel like the solution might be fairly obvious, but I have been stuck on this for quite a while now.


Solution

  • This is because R is in the user's PATH, but sudo ignores it and only looks in a few system locations. This can be solved easily by resolving the full path at the user level:

      - sudo $(which R) CMD javareconf
    

    This inserts the full path to R into the sudo command.

    Note that the javareconf script seems smart enough to detect Java in the standard locations, so it isn't necessary to export JAVA_HOME, PATH or LD_LIBRARY_PATH before calling it.

    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