Include jar file when creating an R package

2019-02-09 11:14发布

问题:

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.

回答1:

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



标签: java r rjava