使用rJava从R中使用Jackcess。

时间:2023-01-20 19:24:29

I am not much familiar with Java but I try to accomplish this task in R (my fav)!

我对Java不是很熟悉,但是我试着在R(我最喜欢的)中完成这个任务!

There is this Java library called Jackcess. I want to connect to this library and open an MS Access 2003 .mdb file in it. Jackcess cookbook tells me the first step to using this library is this:

这里有一个名为Jackcess的Java库。我想连接到这个库,并打开一个MS Access 2003 .mdb文件。Jackcess cookbook告诉我使用这个库的第一步是:

Database db = DatabaseBuilder.open(new File("mydb.mdb"));

or as @Gord suggests,

或@Gord建议,

File file = new File("C:/Users/Public/jackcessTest.mdb");
DatabaseBuilder dbbo = new DatabaseBuilder();
dbbo.setFile(file);
Database db = dbbo.open();

but I'm stuck at this very first step.

但我还是坚持第一步。

I have installed Java and rJava and set up everything about directories. This is my code in R

我已经安装了Java和rJava,并设置了有关目录的所有内容。这是R中的代码。

library(rJava)

.jinit()
.jaddClassPath("java/jackcess-2.1.2.jar") # there I have put the downloaded jar file of Jackcess

# .jaddClassPath("java/commons-logging-1.2.jar") # this is the commons-logging class that Jackcess depends on, commented to replicate problem 2] in my question.



file.name <- "D:/63.mdb" # some data base .mdb file (containing only tables)

file <- .jnew("java/io/File",file.name)
dbbo <- .jnew("com/healthmarketscience/jackcess/DatabaseBuilder")

[Edit: I found out I had two problems, one solved, one still not.] up to this part everything is ok, but I have some problems from now on:

[编辑:我发现我有两个问题,一个解决了,一个还没有。4 .在这个问题上,一切都很好,但从现在起我有一些问题

1] Correctly calling a method from Jackcess without signature mismatch, neither of these work:

1 .正确地将方法从Jackcess调用而没有签名不匹配,这两种工作都没有:

dbbo <- .jcall(dbbo,"L<DatabaseBuilder>","setFile",file)
dbbo <- .jcall(dbbo,"Lcom/healthmarketscience/jackcess/DatabaseBuilder","setFile",file)

I get this error:

我得到这个错误:

Error in .jcall(dbbo, "Lcom/healthmarketscience/jackcess/DatabaseBuilder",  : 
method setFile with signature (Ljava/io/File;)Lcom/healthmarketscience/jackcess/DatabaseBuilder not found

well I found the answer to this step, I just needed a semicolon (;) at the end of class definition string.

我找到了这个步骤的答案,在类定义字符串的末尾,我只需要一个分号(;)

dbbo <- .jcall(dbbo,"Lcom/healthmarketscience/jackcess/DatabaseBuilder;","setFile",file)

2] Calling the open method correctly, my first round of try:

2 .正确调用open方法,第一轮尝试:

 db <- .jcall(dbbo,"Lcom/healthmarketscience/jackcess/Database;","open",evalArray = FALSE,evalString = FALSE)

and I get this error:

我得到了这个错误:

 Error in .jcall(dbbo, "Lcom/healthmarketscience/jackcess/Database;", "open",  : 
 java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

I googled and found out that Jackcess depends on some library called commons-logging, so downloading and adding it to classpath solves THAT problem

我在google上搜索并发现,Jackcess依赖于一些名为common -logging的库,所以下载并将它添加到类路径可以解决这个问题。

3] Calling the open method correctly, my second round of try: with commons-logging in classpath

正确地调用open方法,我的第二轮尝试:在类路径中使用common -logging。

db <- .jcall(dbbo,"Lcom/healthmarketscience/jackcess/Database;","open",evalArray = FALSE,evalString = FALSE)

this gives me this error:

这给了我这个错误:

Error in .jcall(dbbo, "Lcom/healthmarketscience/jackcess/Database;", "open",  : 
java.lang.NoClassDefFoundError: Could not initialize class com.healthmarketscience.jackcess.impl.DatabaseImpl

Any Ideas for this error?

对这个错误有什么想法吗?

[NOTE]: some answers were suggested before my edits, so they may seem irrelevant now, but I have used them in the steps I explained above.

[注释]:在我的编辑之前,有人建议了一些答案,所以它们现在看起来似乎无关紧要,但我已经在上面解释的步骤中使用了它们。

1 个解决方案

#1


0  

The following code shows an alternate approach in Java using the .setFile and .open methods of a "real" DatabaseBuilder object:

下面的代码显示了Java中使用.setFile和.open方法的替代方法。

File file = new File("C:/Users/Public/jackcessTest.mdb");
DatabaseBuilder dbbo = new DatabaseBuilder();
dbbo.setFile(file);
Database db = dbbo.open();

Try something similar in rJava and see if it works for you.

在rJava中尝试类似的方法,看看它是否适合您。

Edit re: updated question

编辑re:更新问题

You mentioned that you added Apache commons-logging to your CLASSPATH, but Jackcess also relies on Apache commons-lang v2.x (not v3.x), so try downloading that and including it in your CLASSPATH as well.

您提到,您将Apache common -logging添加到您的类路径中,但是Jackcess也依赖于Apache commons-lang v2。x(不是v3.x),因此尝试下载它,并将它包含在您的类路径中。

#1


0  

The following code shows an alternate approach in Java using the .setFile and .open methods of a "real" DatabaseBuilder object:

下面的代码显示了Java中使用.setFile和.open方法的替代方法。

File file = new File("C:/Users/Public/jackcessTest.mdb");
DatabaseBuilder dbbo = new DatabaseBuilder();
dbbo.setFile(file);
Database db = dbbo.open();

Try something similar in rJava and see if it works for you.

在rJava中尝试类似的方法,看看它是否适合您。

Edit re: updated question

编辑re:更新问题

You mentioned that you added Apache commons-logging to your CLASSPATH, but Jackcess also relies on Apache commons-lang v2.x (not v3.x), so try downloading that and including it in your CLASSPATH as well.

您提到,您将Apache common -logging添加到您的类路径中,但是Jackcess也依赖于Apache commons-lang v2。x(不是v3.x),因此尝试下载它,并将它包含在您的类路径中。