Running simple `Renjin` example on net-beans, with Java code : "Using R within a Java project"

StackOverflow https://stackoverflow.com/questions/23592839

  •  20-07-2023
  •  | 
  •  

Pergunta

Im tring to run this example from the Renjin website, http://www.renjin.org/documentation/developer-guide.html , im tring to run the first "A simple primer" example.

following is my directory layout:

enter image description here

And here is my code:

package stackoverflow;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import org.renjin.sexp.*; // <-- import Renjin's object classes
/**
 *
 * @author yschellekens
 */
public class StackOverflow {  

   public static void main(String[] args) throws Exception {
   ScriptEngineManager factory = new ScriptEngineManager();
    // create a Renjin engine
    ScriptEngine engine = factory.getEngineByName("Renjin");
    // evaluate R code from String, cast SEXP to a DoubleVector and store in the 'res' variable
    DoubleVector res = (DoubleVector)engine.eval("a <- 2; b <- 3; a*b");
    System.out.println("The result of a*b is: " + res);     

    }
}

Why am i getting the following Exception? (i should get of 6)

run:
Exception in thread "main" java.lang.NullPointerException
    at stackoverflow.StackOverflow.main(StackOverflow.java:22)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

thanks in advance

Foi útil?

Solução

The exception is throw because your application can't find the Renjin ScriptEngine. You have provided renjin-studio as a library, but you need the renjin-script-engine library which is available from http://build.bedatadriven.com/job/renjin/lastSuccessfulBuild/org.renjin$renjin-script-engine/ (use the JAR with dependencies).

Unfortunately ScriptEngineManager.getEngineByName() only returns null if it can't find the engine so you can add the following check to ensure that the engine has loaded:

// check if the engine has loaded correctly:
if(engine == null) {
    throw new RuntimeException("Renjin Script Engine not found on the classpath.");
}

Also note: it is called Renjin, not Rengin!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top