0

Why is the R_HOME Path variable not being set correctly?

public class insights_test {
public static void main(String[] args) throws REXPMismatchException, IOException {

    //System.out.println(System.getProperty("java.library.path"));

    try{
    //rt.exec("/bin/bash -c 'R CMD javareconf -e'"); //THIS SETS $R_HOME

    //View R path in Rstudio: Sys.getenv('R_HOME')
    Process p = Runtime.getRuntime().exec("/bin/bash -c 'export $R_HOME=/usr/lib/R'");

    Process p2 = Runtime.getRuntime().exec("/bin/bash -c 'R CMD Rserve --no-save'");
    //rt.exec("/bin/bash -c 'R CMD Rserve'");
    }catch(IOException e){
        System.out.println("Cant launch Rserve due to error: "+e.getMessage()+"\n");
        e.printStackTrace();
    }
//------------------------------------------------------------------------------------------------------------------------        
    org.rosuda.JRI.REXP rResponseObject=null;
    try {
        /* Create a connection to Rserve instance running on default port 6311..... server launched via Rstudio
         */
        String[] args3=new String[]{"--no-save"};
        Rengine re=new Rengine(args3, false, new TextConsole());

        re.eval("source(\'/home/king/Desktop/_REPOS/Sentiment360/tools/rJava_test/src/rjava_test/rfile.R\')");
        int[] testList = {1, 2, 3};
        rResponseObject = re.eval("try(eval("+
                "testFunction(testList)"+
                "),silent=TRUE)");

        System.out.println(rResponseObject.asString());
    } 
    catch (Exception e) {
        System.out.println("Exception : "+e.getMessage()+"\n"+rResponseObject.asString()+"\n"); 
        e.printStackTrace();
    }
}

}

ERROR:

run:
R_HOME is not set. Please set all required environment variables before running this program.
#
Unable to start R
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f6dd3063c72, pid=24380, tid=0x00007f6e0328c700
#
# JRE version: OpenJDK Runtime Environment (8.0_131-b11) (build 1.8.0_131-8u131-b11-2ubuntu1.16.04.3-b11)
# Java VM: OpenJDK 64-Bit Server VM (25.131-b11 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  [libR.so+0x136c72]
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/king/Desktop/_REPOS/Sentiment360/tools/rJava_test/hs_err_pid24380.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
/home/king/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 134
BUILD FAILED (total time: 0 seconds)
Rilcon42
  • 9,584
  • 18
  • 83
  • 167

1 Answers1

0

Each Runtime.getRuntime() have a specific scope (same as the parent).

Use ProcessBuilder instead of exec()

Replace :

Process p = Runtime.getRuntime().exec("/bin/bash -c 'export $R_HOME=/usr/lib/R'");
Process p2 = Runtime.getRuntime().exec("/bin/bash -c 'R CMD Rserve --no-save'");

By :

ProcessBuilder pb = new ProcessBuilder("/bin/bash","-c 'R CMD Rserve --no-save'");
Map<String, String> env = pb.environment();
env.put("R_HOME", "/usr/lib/R");
Process p = pb.start();

See this SO question Is it possible to set an environment variable at runtime from Java?

Indent
  • 4,675
  • 1
  • 19
  • 35