FAQ for the R-Java Interface

  1. Installation
  2. R from within Java
  3. Starting the Java Virtual Machine
  4. Performance
  5. Method Identification
  6. Troubleshooting calling R from Java

Insallation

  • When I install the package, everything goes fine, but if I try to use it by calling R from Java, I get a collection of errors about an UnsatisfiedLinkError and missing symbol STRING_ELT. What's going on?
    Almost definitely, this indicates that your version of R is not compiled as a shared library (libR.so or R.dll). Unfortunately, to run R inside another application (e.g. Java), you will need to have R compiled as a shared library. Download the R source, and configure it by adding the option
     --enable-R-shlib
    
    in the call to configure. Then run make. It is a good idea to issue the command
           make distclean
    
    before doing this. This removes any existing libraries which may not be recompiled to link against libR.so.
  • When I try to call R from Java on Windows, it fails giving me something about missing symbol Rf_initEmbeddedR, etc.
    Unfortunately, I have not yet added the code to the Windows version of R to make the initialization of the R session similar to that on Unix. When that is in place, calling R from Java will work as it does on Unix.
  • Java in R or R in Java

    The first few releases of this package followed the regular R package style of allowing the Java facilities to be loaded into a running R session. While this is desirable, for technical reasons, currently the best way to integrate Java and R is to run Java and have it load R.
  • What version of R do I need for this setup?
    You will need version 1.2.0 (currently in development and to be released in early December). This requires R to be built as a shared library (libR.so) and, on Unix, the bin/R_X11.so (or similarly named library) to be linked against this shared library.
  • How do we start Java so that it will run R?
    The most basic way is to run the script scripts/RJava which is is installed under the directory in which the package is installed.
    Other ways include using the omegahat shell script, if it is installed.
  • When I run scripts/RJava, I get a Java exception of the form
    Exception in thread "main" java.lang.UnsatisfiedLinkError: nativeSetup   
    
    What can I do?
    This is probably due to some conflicts with other shared libraries. Unset the value of the environment variable LD_LIBRARY_PATH (unsetenv LD_LIBRARY_PATH in the C/TCSH shells, and unset LD_LIBRARY_PATH in Bash) and try running the script again.
  • How do I pass command line arguments to R?
    Most of the arguments that one can pass to the R command can be passed in the same way.
      RJava --silent --gui=none --no-save
    
    The following list is edited from the output of the R --help command to list the
    --save Do save data sets at the end of the session
    --no-save Don't save them
    --no-environ Don't read the .Renviron or ~/.Renviron files
    --no-site-file Don't read the site-wide Rprofile
    --no-init-file Don't read the .Rprofile or ~/.Rprofile files
    --restore Do restore previously saved data sets at startup
    --no-restore-data Don't restore previously saved data sets
    --no-restore-history Don't restore the R history file
    --no-restore Don't restore anything
    --vanilla Combine --no-save, --no-restore, --no-site-file, --no-init-file and --no-environ
    --no-readline Don't use readline for command-line editing
    --vsize=N Set vector heap size to N bytes; `4M' = 4 MegaB
    --nsize=N Set number of cons cells to N
    -q, --quiet Don't print startup message
    --silent Same as --quiet
    --slave Make R run as quietly as possible
    --verbose Print more information about progress
    -g, --gui=TYPEUse TYPE as GUI; possible values are `X11' (default), `none' and `gnome'
  • Starting the Java Virtual Machine

  • When I load the shared library (Java.so) I get errors about not being able to load a shared library or undefined symbols. What's the problem?
    Most likely, you do not have the environment/shell variable LD_LIBRARY_PATH set correctly. Source one of the RJava.csh or RJava.bash scripts that are created during the configuration of the R-Java package. To find them, run the R command
       system.file("scripts/RJava.csh", pkg="Java") 
    
    The first is for use within C shells (csh and tcsh) and the latter for use with the Bourne shell (sh, bash). This must be done before starting R.

    These scripts append the appropriate directories to one's LD_LIBRARY_PATH variable and makes . the Java libraries and support library in the R package for Java available to the dynamic loader.

  • When I call .JavaInit(), the prompt never returns. Any solution?
    We have seen this on Linux with IBM's JDK1.3. The problem is that when loading the shared library (Java.so) one must specify FALSE as the value for local. That is
            dyn.load("Java.so", local=FALSE)
          
    The automatic configuration attempts to get this correct and put the appropriate call in .First.lib.
  • On Solaris, when I initialize the Java Virtual Machine, I get a paragraph about needing to apply some patches. Do I really have to install these?
    The solution answer again lies with the way dyn.load() is called. Make certain that the Java.so shared library is loaded with the argument local=FALSE.
  • When I try to do anything that involves the class RForeignReference, Java complains about not being able to load a library named Java.so and prints a huge call-stack trace.
    The problem is due to one of two things. Perhaps you have not set your LD_LIBRARY_PATH variable correctly.
    More likely however is that when you installed the package, you did not give the R INSTALL command the -c (or --clean) argument. This is necessary as it tidies the installation up after everything is compiled and made available to R. This creates a symbolic link in the Java/libs directory that connects Java.so expected by R to libJava.so expected by the Java virtual machine. In other words, this issues the command
    ln -s Java.so libJava.so      
          
    in the Java/libs directory. A script - cleanup - will do this for you. It is located in the Java/scripts directory where the library was installed.
  • Why do we have to explicitly run .JavaInit() ? It seems like a waste of time after the library call.
    From Tony Rossini:
    Because you only get one chance per R session to add directories or jars to the classpath, set the library path and start up the Omegahat interpreter (almost!).
  • I forgot to start the JVM (i.e. call .JavaInit()) before calling one of the functions that needed it. Now when I call .JavaInit() after this, it hangs?
    Yep. This appears to be a problem with some operating systems and Java implementations. It is likely that this is a problem with threads and synchronization. I am not certain how to handle it at this stage. See the Bug list.
  • Performance

  • Why is the first call to .Java() or .JavaConstructor so slow?
    Because, the default setup for the Omegahat interpreter currently allows the user to specify partially qualified class names. To do this, the Omegahat interpreter must read every element in the classpath and construct a list of all the classes in all the sub-directories of those elements. This takes time!

    This lengthy class list construction can be reduced (or eliminated). Firstly, use fully-qualified class names. This makes the code more readable, but it is tedious.
    An alternative is to tell Omegahat which classpath elements for which it should construct the class lists. This list is specified via the JVM property OmegahatClassLists

     javaConfig(OmegahatClassLists="./myclasses:/home/me/Java/classes.jar")
    
    See The Omegahat Howto. Additionally, the "production" versions (i.e. the non-development) versions we use typically load these class lists from a serialized object computed in an earlier session and this again reduces the startup time.
  • Event Loops

  • When I run the X11() graphics device and a Java GUI, sometimes I get errors like the following
    Xlib: unexpected async reply (sequence 0x1356)!
    
    1: X11 protocol error: BadDrawable (invalid Pixmap or Window parameter) 
    2: X11 protocol error: BadDrawable (invalid Pixmap or Window parameter) 
    
    What's happening?
    Both Java and R are reading events from the X11 event loop ``at the same time''. As a result, R is reading events meant for Java and vice-versa and bad things happen!

    There are two solutions:

    • use the Java graphics device which uses the Java graphics facilities to do the rendering;
    • don't run the GUI and the X11 device at the same time.
    The second solution is clearly not ideal. What we can do is to compile the X11 device code to be thread-safe. However, the same effect will be seen if we load another X11 event handler (e.g. the Tcl/Tk library).
  • Method Identification

  • Why do I get anonymous references that have class MethodAlias?
    This means that the .Java call couldn't resolve a real Java method, but got one that had the same name and number of arguments. One of the most common causes for this in R is when one specifies an integer constant (e.g. 0) but it is passed to Java as a double/numeric. For example
     v <- .JavaConstructor("util.Vector", 3)
    
    The three here is actually of mode numeric, not integer.

    This behaviour is part of R and S3 and unfortunately we are stuck with it. (A lot of the R code (especially Fortran associated) breaks if this is changed. It does not happen in S4.)

  • Troubleshooting calling R from Java

    Question and answer contributed by Lars Schmidt-Thieme. Thanks.

    Problem:
    Trying the example for calling R from Java via

       SJava/scripts/RJava --example --gui-none
    
    (in <RHOME>/library) throws an exception
      Loading RInterpreter library
      Exception in thread "main" java.lang.UnsatisfiedLinkError: no RInterpreter in java.library.path
              at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1410)
              at java.lang.Runtime.loadLibrary0(Runtime.java:772)
              at java.lang.System.loadLibrary(System.java:832)
              at org.omegahat.R.Java.ROmegahatInterpreter.(ROmegahatInterpreter.java:34)
              at org.omegahat.R.Java.Examples.JavaRPrompt.main(JavaRPrompt.java:17)
    
    (or similiar; sometimes SJava instead of RInterpreter may be missing).

    Rationale:
    In <RHOME>/library/SJava/libs the symlinks from libRInterpreter.so and libSJava.so to SJava.so are missing, probably, because you did not install SJava using the -c command switch.

    Solution:
    Reinstall SJava using the -c switch, i.e.

      R CMD INSTALL -c SJava_0.62-6.tar.gz
    
    or create the symlinks by hand, switching to directory <RHOME>/library/SJava/libs and saying
      ln -s SJava.so  libRInterpreter.so
      ln -s SJava.so libSJava.so
    

    Windows

    Compiling


    Last modified: Mon Feb 4 08:18:43 EST 2002