Search code examples
javarmavenjakarta-eercaller

java.lang.NoClassDefFoundError: rcaller/RCaller in java-ee


I am trying to integrate regression R code into my j2ee project using R caller. My datasource is from mysql database. So I used RMYSQL as package. My code work fine under java project but when I moved it into backing bean class it throws an exception: java.lang.NoClassDefFoundError: rcaller/RCaller

In maven dependency it exist only version 2.8 of Rcaller. And I'm in need to 2.5 version so I haved added it to build path manually.

The backing bean method is:

try {

       RCaller caller = new RCaller();
       RCode code = new RCode();  

       caller.setRscriptExecutable("Rscript.exe");
       code.clear();
       caller.setRCode(code);

       code.R_require("RMySQL");
       code.R_require("rpart");


       code.addRCode("mydb= dbConnect(MySQL(),user='root',password='root',dbname='db',host='localhost')"); 
       code.addRCode("rs= dbSendQuery(mydb,'select * from table')"); 
       code.addRCode("data = fetch(rs,n=-1)");  
       code.addRCode("data= data[data[['cred']]>0,]");  
       code.addRCode("data$navig <- ifelse(data$navig == 'Chrome',1,ifelse(data$navig == 'Firefox',2,ifelse(data$navig == 'Android',3,ifelse(data$navig == 'iPhone',4,9))))");  
       code.addRCode("data$rate =as.factor(data$rate)");  
       code.addRCode("ad.apprentissage= rpart(rate~vqs+ibt+tbt+bf+n+down+ping, data=data,minsplit = 7)");  
       code.addRCode("predArbreDecision=predict(ad.apprentissage,newdata=data,type='class') ");  
       code.addRCode("table(data$rate, predArbreDecision)");  


       File file = code.startPlot();
      // code.addRCode("ggplot(df, aes(x, y)) + geom_point() + geom_smooth(method='lm') + ggtitle('y = f(x)')");
code.addRCode("plot(ad.apprentissage,main='Arbre de décision de la vidéo Streaming')");
code.addRCode("text(ad.apprentissage)");
       caller.runOnly();
       ImageIcon ii = code.getPlot(file);
       code.showPlot(file);

   } catch (Exception e) {
       System.out.println(e.toString());
   } 

And in order to display the graph in jsf page, I have called that function as following:

 #{video_R_IntegrationBean.Test3()} 

Solution

  • Since you are using maven, you have to follow the maven way.

    If you have this jar of 2.5 version, please install it either in your local repository, or in a custom repository. Please see here

    mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file  -Dfile=path-to-your-artifact-jar \
                                                                                  -DgroupId=your.groupId \
                                                                                  -DartifactId=your-artifactId \
                                                                                  -Dversion=version \
                                                                                  -Dpackaging=jar \
                                                                                  -DlocalRepositoryPath=path-to-specific-local-repo
    

    Once it is installed in your repository, you have to declare it in your pom.xml as dependency and of course dont forget to reference your local repository in repositories section.