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

时间:2019-03-31 12:05:23

标签: java r intellij-idea jar project

我正在努力使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)

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

尽管我支持第一个选择,但也许由于我对Java相对较新,所以缺少了一些东西。

2 个答案:

答案 0 :(得分:0)

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

这将为执行jar设置名为R_HOME的环境变量

答案 1 :(得分:0)

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

await

如果R_HOME包含空格或减号,则报价很重要。