Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include jar file when creating an R package

Tags:

java

r

rjava

I created a skeleton R package:

lib
 jarFileHere.jar 
R
 r_code_file.R

The r_code_file.R tries to references a class file in jarFileHere.jar:

library("rJava")
library("rjson")

.onLoad <- function(libname, pkgname) {
  .jpackage(pkgname, lib.loc=libname)
}

.onLoad("packagename", "../lib/jarFileHere.jar")

.jnew("com/test/ClassHere", "") 

But I get a failure due to java.lang.NoClassDefFoundError.

I was able to get it to work using

.jinit
.jaddClassPath("../lib/jarFileHere.jar")

but the rJava docs explicitly says not to use .jinit because it won't work when the code is used as a package.

like image 370
tommy chheng Avatar asked Oct 02 '10 00:10

tommy chheng


1 Answers

Make your structure like:

inst
 java
  jarFileHere.jar 
R
 r_code_file.R

For examples on how to include java in your package look at the helloJavaWorld package.

Also take a look at the source of Deducer and DeducerplugInExample. There is a tutorial on including java code in your package available on Deducer's web manual: http://www.deducer.org/pmwiki/pmwiki.php?n=Main.Development#suaptijc

like image 184
Ian Fellows Avatar answered Oct 17 '22 09:10

Ian Fellows