0% found this document useful (0 votes)
220 views9 pages

Rjava.R: .Jcall Doent Works, As Stacktrace Below: Error in .Jcall (Obj Obj, Returnsig "V", Method "Gen", "A", "B")

The documents describe steps to compile and package a Java class into a JAR file and copy the JAR and dependencies to an R project folder for use in R. The Java class scrapes sales data from a website, transforms it, and writes the output to a CSV file. When called from R, initialization and method calls on the Java class fail due to classpath issues.

Uploaded by

JoãoBoscoJares
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
220 views9 pages

Rjava.R: .Jcall Doent Works, As Stacktrace Below: Error in .Jcall (Obj Obj, Returnsig "V", Method "Gen", "A", "B")

The documents describe steps to compile and package a Java class into a JAR file and copy the JAR and dependencies to an R project folder for use in R. The Java class scrapes sales data from a website, transforms it, and writes the output to a CSV file. When called from R, initialization and method calls on the Java class fail due to classpath issues.

Uploaded by

JoãoBoscoJares
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

rJava.

R
[Link](JAVA_HOME="C:/Program Files/Java/jdk1.7.0_71/jre")
if(!("rJava" %in% rownames([Link]()))){
[Link]("rJava")
}

javaCompileAndPackageToRFolder.R
library(rJava)

compileAndPackagejavaToRFolder <- function(massgenFolder){


rProjectMainFolder <- getwd()
setwd(massgenFolder)
system("mvn clean dependency:copy-dependencies package",[Link] = TRUE)

if([Link]("[Link]")){
[Link]("[Link]")
}
[Link](paste0(getwd(),"/","target/","[Link]"),
paste0(rProjectMainFolder),overwrite = TRUE)
if(![Link](paste0(rProjectMainFolder,"/","lib"))){
[Link](paste0(rProjectMainFolder,"/","lib"),
showWarnings = TRUE, recursive = FALSE)
}
DirSource <- paste0(getwd(),"/","target/","dependency/*.*")
DirEnd <- paste0(rProjectMainFolder,"/","lib/")
[Link]( paste0(getwd(),"/","target/","dependency"),
paste0(rProjectMainFolder,"/","lib"), overwrite = TRUE)
system(paste("cp -r", DirSource, DirEnd))
setwd(rProjectMainFolder)
}
compileAndPackagejavaToRFolder()

TestJavaCall.R
source("rJava.R")
source("javaCompileAndPackageToRFolder.R")
library(rJava)

executeAMethod <- function(){


.jinit("[Link]",parameters="-Xmx512m")
obj = .jnew(class = "com/xxxxx/massgen/ITunesSalesDataGen",check = TRUE)

.jcall(obj = obj,returnSig = "V",method = "gen","a","b")

.jcall doent works, as stacktrace below:


Error in .jcall(obj = obj, returnSig = "V", method = "gen", "a", "b") :

method gen with signature (Ljava/lang/String;Ljava/lang/String;)V not found


obj$gen(paste0(gsub("\\\\", "/", getwd()),"/[Link]")
,paste0(gsub("\\\\", "/", getwd()),"/dependency/[Link]"))

obj$gen doent works, as stacktrace below:


Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, :
[Link]: org/jsoup/Jsoup
}
executeAMethod()

Java Class
import
import
import
import
import
import
import
import

[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];

import
import
import
import
import
import

[Link];
[Link];
[Link];
[Link];
[Link];
[Link];

import [Link];
@Service
public class ITunesSalesDataGen {

/** I tried use static method, change argument into string array... **/
public void gen(String input, String output) {
try{
List<ITunesDigitalSalesData> result = new ArrayList<ITunesDigitalSalesData>();
ITunesDigitalSalesData itunesDigitalSalesData = null;
String iTunesFilePath = input;
FileInputStream fis = null;
if(iTunesFilePath==null || "".equals(iTunesFilePath)){
throw new RuntimeException("Please, input the filepath argument.");
}else{
fis = new FileInputStream(new File(iTunesFilePath));
}
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
[Link]();
String line;
StringBuffer sb = new StringBuffer();
[Link]("ID");
[Link](";");
[Link]("COUNTRY");
[Link](";");
[Link]("DATE");

[Link](";");
[Link]("RANK");
[Link](";");
[Link]("ARTIST");
[Link](";");
[Link]("TITLE");
[Link](";");
[Link]("SALES");
[Link]("\n");
while ((line = [Link]()) != null) {
String[] lineAttributes = [Link](";");
String countryNumber = lineAttributes[0];
String country = lineAttributes[1];

Document doc = [Link]("[Link]


for (Element table : [Link]("div").select("table")) {
for (Element row : [Link]("tr")) {
Elements tds = [Link]("td");
if ([Link]() > 0) {
if("RANK".equals([Link](0).text())){
continue;
}
[Link](countryNumber);
[Link](";");
[Link](country);
[Link](";");
[Link](new SimpleDateFormat("dd/MM/yyyy").format(new Date()));
[Link](";");
[Link]([Link](0).text());
[Link](";");
[Link]([Link](1).text());
[Link](";");
[Link]([Link](2).text());
[Link](";");
[Link]([Link](3).text());
[Link]("\n");
}
}
}
}
String iTunesFilePathResult = output;
[Link](new File(iTunesFilePathResult),[Link](),"UTF-8");
}catch(Exception e){
throw new RuntimeException([Link](),e);
}
}
}

The output
source("testJavaCall.R")

[INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for
[Link]:MassGen:jar:0.0.1-SNAPSHOT [WARNING] '[Link].(groupId:artifactId:type:classifier)' must be unique:
[Link]:httpclient:jar -> version 4.3.5 vs 4.4.1 @ line 227, column 15 [WARNING] '[Link]' for
[Link]:maven-jar-plugin is missing. @ line 30, column 21 [WARNING] [WARNING] It is highly recommended to fix these

problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support
building such malformed projects. [WARNING] [INFO]
[INFO] ------------------------------------------------------------------------ [INFO] Building MassGen 0.0.1-SNAPSHOT [INFO] ----------------------------------------------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MassGen --- [INFO] Deleting
C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target [INFO] [INFO] --- maven-dependency-plugin:2.8:copy-dependencies (default-cli) @
MassGen --- [INFO] Copying [Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO]
Copying [Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to
C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to
C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to
C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to
C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to
C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to
C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to
C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to
C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link]
to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to
C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to
C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link]
[INFO] Copying [Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying
[Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying
[Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO]
Copying [Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO]
Copying [Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to
C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO]
Copying [Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to
C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying
[Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] Copying [Link] to
C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\dependency\[Link] [INFO] [INFO] --- maven-resources-plugin:2.6:resources
(default-resources) @ MassGen --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 585 resources [INFO] [INFO] --maven-compiler-plugin:3.1:compile (default-compile) @ MassGen --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 31
source files to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources
(default-testResources) @ MassGen --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MassGen --- [INFO] Changes detected - recompiling the module! [INFO] Compiling
1 source file to C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test
(default-test) @ MassGen --- [INFO] Surefire report directory: C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\surefire-reports

TESTS
Running [Link] SLF4J: Failed to load class "[Link]". SLF4J: Defaulting to nooperation (NOP) logger implementation SLF4J: See [Link] for further details. May 15, 2015 [Link]
PM [Link] log INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE,
requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=50} May 15, 2015 [Link] PM
[Link] log INFO: Opened connection [connectionId{localValue:1, serverValue:201}] to localhost:27017 May

15, 2015 [Link] PM [Link] log INFO: Monitor thread successfully connected to server with description
ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[2, 6, 4]},
minWireVersion=0, maxWireVersion=2, maxDocumentSize=16777216, roundTripTimeNanos=682327} started May 15, 2015 [Link] PM
[Link] log INFO: Opened connection [connectionId{localValue:2, serverValue:202}] to localhost:27017
finished Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.358 sec
Results :
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MassGen --- [INFO] Building jar:
C:\Users\JoaoBoscoJares\workspace\xxxxx\MassGen\target\[Link] [INFO] -----------------------------------------------------------------------

BUILD SUCCESS
[INFO] ------------------------------------------------------------------------ [INFO] Total time: 11.661 s [INFO] Finished at: 2015-05-15T[Link]+01:00
[INFO] Final Memory: 23M/218M [INFO] ------------------------------------------------------------------------

Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, :


[Link]: org/jsoup/Jsoup

The [Link]
<project xmlns="[Link] xmlns:xsi="[Link]
xsi:schemaLocation="[Link] [Link]
<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>MassGen</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<[Link]>[Link]</[Link]>
<[Link]>UTF-8</[Link]>
<[Link]>1.7</[Link]>
<[Link]>1.7</[Link]>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${[Link]}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>

</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>sifem3</id>
<name>sifem</name>
<url>[Link]
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>apache-repo-snapshots</id>
<url>[Link]
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>bedatadriven</id>
<name>bedatadriven public repo</name>
<url>[Link]
</repository>
<repository>
<id>rforge</id>
<name>rforge public repo</name>
<url>[Link]
</repository>
<repository>
<id>mjorm-webdav-maven-repo</id>
<name>mjorm maven repository</name>
<url>[Link]
<layout>default</layout>
</repository>
<repository>
<id>central</id>
<url>[Link]
</repository>
</repositories>
<dependencies>
<!-- Spring -->

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-core</artifactId>
<version>${[Link]}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-context</artifactId>
<version>${[Link]}</version>
<scope>compile</scope>
<exclusions>

<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-web</artifactId>
<version>${[Link]}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${[Link]}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-aspects</artifactId>
<version>${[Link]}</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-tx</artifactId>
<version>${[Link]}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-orm</artifactId>
<version>${[Link]}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-test</artifactId>
<version>${[Link]}</version>
<scope>compile</scope>
</dependency>

<!-- mongo -->


<dependency>
<groupId>[Link]</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>[Link]</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-data-commons-core</artifactId>
<version>[Link]</version>
<scope>compile</scope>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- apache http components -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>httpmime</artifactId>
<version>4.4.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>httpclient</artifactId>
<version>4.4.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>[Link]-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

You might also like