title: Run R script using JAVA program
body: <p>I am new to R programming. I have created a simple R script and trying to run it using JAVA class, but I am unable to do it.</p>
  
  <p>I have tried by using Rserve as well as rJava. Using Rserve, code execution stopped after creating instance of "RConnection" whereas using rJava giving exception "java.lang.UnsatisfiedLinkError: jri.dll: Can't find dependent libraries".</p>
  
  <p>The JAVA class code is as below:</p>
  
  <p>For rJava:</p>
  
  <pre><code>    import org.rosuda.JRI.Rengine;
  public class Temp {
  
  public static void main(String a[]) {
      // Create an R vector in the form of a string.
      String javaVector = "c(1,2,3,4,5)";
  
      // Start Rengine.
      Rengine engine = new Rengine(new String[] { "--no-save" }, false, null);
  
      // The vector that was created in JAVA context is stored in 'rVector' which is a variable in R context.
      engine.eval("rVector=" + javaVector);
  
      //Calculate MEAN of vector using R syntax.
      engine.eval("meanVal=mean(rVector)");
  
      //Retrieve MEAN value
      double mean = engine.eval("meanVal").asDouble();
  
      //Print output values
      System.out.println("Mean of given vector is=" + mean);
  
  }
  }
  </code></pre>
  
  <p>For Rserve:</p>
  
  <pre><code>import org.rosuda.REngine.REXPMismatchException;
  import org.rosuda.REngine.Rserve.RConnection;
  import org.rosuda.REngine.Rserve.RserveException;
  public class Temp {
  
  public static void main(String a[]) {
      RConnection connection = null;
      System.out.println("line 10");
      try {
          // Create a connection to Rserve instance running on default port 6311
  
          System.out.println("line 15");
          connection = new RConnection();
          System.out.println("line 17");
          //Note four slashes (\\\\) in the path
          connection.eval("source('D:\\\\RExamples\\\\helloworld.R')");
          System.out.println("line 19");
          int num1=10;
          int num2=20;
          int sum=connection.eval("myAdd("+num1+","+num2+")").asInteger();
          System.out.println("The sum is=" + sum);
      } catch (RserveException e) {
          e.printStackTrace();
      } catch (REXPMismatchException e) {
          e.printStackTrace();
      }
  }
  }
  </code></pre>
  
  <p>Please let me know if my question is not clear to you or if you want to know anything else. Thanks in advance.</p>