有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

classpath Linux path变量未在Java程序中正确设置

为什么R_HOME Path变量设置不正确

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();
    }
}

}

错误:

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)

共 (1) 个答案

  1. # 1 楼答案

    每个运行时。getRuntime()具有特定的作用域(与父级相同)

    使用ProcessBuilder而不是exec()

    替换:

    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'");
    

    作者:

    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();
    

    看到这个问题了吗Is it possible to set an environment variable at runtime from Java?