[R] [rJava] RJavaClassLoader and system classloader

Benoît Thiébault thiebault at artenum.com
Fri Sep 26 13:55:45 CEST 2014

Hi everyone,

I want to call a Java application from R and have encountered some problems with the way rJava deals with the system class loader.

To run my application, I use the following R script:

> library(rJava)
> .jinit()
> .jaddClassPath("myApp.jar")
> rWrapper <- .jnew("org/test/RWrapper")
> .jcall(rWrapper,"V","start")

My Java application has a plugins loading mechanism that uses a specific PluginClassLoader to load plugins stored in additional JAR files (e.g. plugin.jar). This PluginClassLoader is programed so that it knows and loads the plugins JARs. As any Java classloader, it is a child of the system class loader.

Finally, plugins not only use classes stored in their plugin.jar file, they also depend on classes contained in the main myApp.jar file (the Plugin interface is for example defined in the myApp.far)

In a pure Java environment, myApp.jar is known from the system class loader (it is in the classpath) and thus the PluginClassLoader can load classes from the plugin (it knows both about the plugin.jar and the myApp.jar files)

In the R context however, using the above script, its the RJavaClassLoader that knows about the myApp.jar file. The org.test.RWrapper class is instatiated by the RJavaClassLoader. The system class loader however does not know about myApp.jar anymore. Neither does the PluginClassLoader. So when the PluginClassLoader loads a plugin class, it can only load classes that are in the plugin.jar file and as soon as a class from the myApp.jar file is required by the plugin, it crashes with a java.lang.NoClassDefFoundError message.

My question is: how can I force rJava to load the classpath in the system classloader and not only in the RJavaClassLoader?

I cannot make the PluginClassLoader know the RJavaClassLoader as my application also has to run in a non-R environment.

Thanks for your time,

Kind regards,


More information about the R-help mailing list