简体   繁体   中英

How to make environment variables work in a JAR file?

I am struggling with making RJava work in my Java program, as it seems to depend on the whole R directory tree to work. This directory tree is named $R_HOME in the system and, by configuring the project's environment variables, it points to /usr/lib64/R .

This works OK when I run my application on my IDE (IntelliJ IDEA), but whenever I try to pack my code in a JAR and run it using java -jar xxx.jar , it will throw the following error:

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=0x00007f847c3a470c, pid=4232, tid=4233
#
# JRE version: OpenJDK Runtime Environment (10.0.2+13) (build 10.0.2+13)
# Java VM: OpenJDK 64-Bit Server VM (10.0.2+13, mixed mode, tiered, compressed oops, g1 gc, linux-amd64)
# Problematic frame:
# C  [libR.so+0x17270c]
#
# Core dump will be written. Default location: Core dumps may be processed with "/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %h %e" (or dumping to /home/xvlaze/IdeaProjects/Ypsilon/out/artifacts/Ypsilon/core.4232)
#
# An error report file with more information is saved as:
# /home/xvlaze/IdeaProjects/Ypsilon/out/artifacts/Ypsilon/hs_err_pid4232.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.
#
Aborted (core dumped)

That message was the one I was getting on the IDE while the R_HOME environment variable was not set. Seems either the JAR file can't read any environment variables or that I need to tell the project it has to include something I don't know.

Although I'm rooting for the first option, perhaps I am missing something as I am relatively new to Java.

Simplest solution is to run it as: R_HOME=/usr/lib64/R java -jar xxx.jar

This would set an environment variable named R_HOME for the execution of your jar

Run your app with -D argument:

java -DR_HOME="/path/to/some-dir" -jar xxx.jar

Quotes are important if R_HOME contains spaces or minuses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM