如何使环境变量在 JAR 文件中工作?
How to make environment variables work in a JAR file?
我正在努力让 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)
该消息是我在 IDE 上收到的消息,而 R_HOME 环境变量未设置。似乎 JAR 文件无法读取任何环境变量,或者我需要告诉项目它必须包含我不知道的内容。
虽然我支持第一个选项,但也许我遗漏了一些东西,因为我对 Java 比较陌生。
最简单的解决方案是运行它作为:R_HOME=/usr/lib64/R java -jar xxx.jar
这将为执行您的 jar
设置一个名为 R_HOME
的环境变量
运行 您的带有 -D 参数的应用程序:
java -DR_HOME="/path/to/some-dir" -jar xxx.jar
如果 R_HOME 包含空格或减号,引号很重要。
我正在努力让 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)
该消息是我在 IDE 上收到的消息,而 R_HOME 环境变量未设置。似乎 JAR 文件无法读取任何环境变量,或者我需要告诉项目它必须包含我不知道的内容。
虽然我支持第一个选项,但也许我遗漏了一些东西,因为我对 Java 比较陌生。
最简单的解决方案是运行它作为:R_HOME=/usr/lib64/R java -jar xxx.jar
这将为执行您的 jar
设置一个名为R_HOME
的环境变量
运行 您的带有 -D 参数的应用程序:
java -DR_HOME="/path/to/some-dir" -jar xxx.jar
如果 R_HOME 包含空格或减号,引号很重要。