Nick Trileski
Nick Trileski

Reputation: 151

RJDBC not loading as a library in Rapache

So I need to get access to an MSSQL server in my Rapache program and when I try to load RJDBC as a library in my Rapache code I get server error. In the logs it looks like this:

    referer: http://10.21.8.145/
    Error : .onLoad failed in loadNamespace() for 'rJava', details:
      call: dyn.load(file, DLLpath = DLLpath, ...)
      error: unable to load shared object '/usr/lib/R/site-library/rJava/libs/rJava.so':
      libjvm.so: cannot open shared object file: No such file or directory
    Error: package/namespace load failed for 'rJava'
 Traceback:
    5: stop(gettextf("package/namespace load failed for %s", sQuote(package)), 
   call. = FALSE, domain = NA)
    4: library(rJava)
    3: eval.with.vis(expr, envir, enclos)
    2: eval.with.vis(ei, envir)
    1: source("/var/www/brew/optimization.R")
    [Fri Jun 15 13:57:29 2012] [error] [client 10.21.2.79] File does not exist:  /var/www/favicon.ico

Also I checked and rJava.so exists in that directory. Is it something with libjvm.so?

The thing is when I run this library(RJDBC) in just R it says this:

    Loading required package: DBI
    Loading required package: rJava
    Warning message:
    replacing previous import 'show' when loading 'rJava'

and then all my RJDBC code aftewards works fine. So is this a problem with some Java configuration or is this a problem with rApache not being able to run the library because of the warning message?

Upvotes: 0

Views: 484

Answers (2)

oddHypothesis
oddHypothesis

Reputation: 195

I had the same issue and determined the cause to be that rApache was not setting the LD_LIBRARY_PATH environment variable. This needs to be set before an R environment is created for rJava (or any packages that depend on it) to initialize correctly (i.e. be able to find the path to libjvm.so).

You can check this with a simple test script:

setContentType('text/text')
cat(Sys.getenv()['LD_LIBRARY_PATH'], '\n')
DONE

Unfortunately, the simple fix using a SetEnv directive to Apache doesn't work. Instead you need to do the following:

(Note the following was done on a 64-bit RHEL5 Server)

Add a file to /etc/ld.so.conf.d called rApache_rJava.conf with the following contents:

/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/amd64/server/

(the above line was the path to the parent directory of libjvm.so on my server)

As root:

$ /sbin/ldconfig

followed by restarting Apache

After Apache restarts you should be able to run R-scripts via rApache that depend on rJava, e.g. anything with lines like:

library(rJava)

The process above with additional reference links is also outlined here, on my blog: http://oddhypothesis.blogspot.com/2012/06/making-rapache-load-rjava.html

EDIT: Full Disclosure

The blog linked to above is my own

Upvotes: 0

Jeff
Jeff

Reputation: 1426

Yes, it is something with libjvm.so ;) rjava.so cannot find it!

Various linux distros (I presume you're running linux?) will start apache in a restricted environment thus limiting where the process can find shared libraries. I recommend looking through the shell scripts to see if this is happening.

I'd also investigate your dynamic linker run-time binding configuration, e.g., the /etc/ld.so.* files along with the ldconfig command.

Try running your code from your personal R session. If it doesn't run there, then you need to fix your java/rJava install first. If it does run there, then explore the topics I discussed above.

Oh, and I just remembered: rJava may source in some java specific environment variables, located in the rJava package installation. See if that's happening in your local session. If that's the case then you will most likely need these set when apache/rApache is run.

And as I mentioned above, if your distro is restricting apache's environment, then you'll have to shim those Java envirnment variables into that restriction process.

Cheers!

Jeff

Upvotes: 1

Related Questions