如何使环境变量在 JAR 文件中工作?

xvl*_*aze 2 java r jar project intellij-idea

我正在努力使 RJava 在我的 Java 程序中工作,因为它似乎依赖于整个 R 目录树才能工作。这个目录树在系统中被命名为 $R_HOME ,通过配置项目的环境变量,它指向/usr/lib64/R.

当我在我的 IDE (IntelliJ IDEA) 上运行我的应用程序时,这可以正常工作,但是每当我尝试将我的代码打包到 JAR 中并使用 运行它时java -jar xxx.jar,它会抛出以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

该消息是我在未设置 R_HOME 环境变量时在 IDE 上收到的消息。似乎 JAR 文件无法读取任何环境变量,或者我需要告诉项目它必须包含我不知道的内容。

尽管我支持第一个选项,但由于我对 Java 比较陌生,因此我可能遗漏了一些东西。

Shl*_*oim 5

最简单的解决方案是将其运行为: R_HOME=/usr/lib64/R java -jar xxx.jar

这将设置一个以R_HOME执行 jar命名的环境变量


Den*_*nis 5

使用 -D 参数运行您的应用程序:

java -DR_HOME="/path/to/some-dir" -jar xxx.jar
Run Code Online (Sandbox Code Playgroud)

如果 R_HOME 包含空格或减号,引号很重要。