bash Java 无法获取环境变量(系统属性)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28002327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:12:08  来源:igfitidea点击:

Java cannot get environment variable (system property)

javabashubuntuintellij-idea

提问by quarks

I need that my code can get the environment variable when running test with IntelliJ:

我需要在使用 IntelliJ 运行测试时我的代码可以获取环境变量:

private String sdkDir = System.getProperty("sdk.root");

The problem is that this is always null, I have set both in bashrcand also with bash_profilebut not really works with Ubuntu.

问题是,这始终是null,我已经在Ubuntu 中进行了设置bashrcbash_profile但并没有真正适用于 Ubuntu。

bashrcand bash_profileboth have this:

bashrc并且bash_profile都有这个:

export JAVA_HOME=/home/xybrek/zulu1.8.0_25-8.4.0.1-x86lx64
export JAVA_OPTIONS="-Dsdk.root=/home/xybrek/java-sdk-1.9.17"
export PATH=$PATH:/home/xybrek/zulu1.8.0_25-8.4.0.1-x86lx64/bin
export PATH

What could be missing with my configuration?

我的配置可能缺少什么?

Update:

更新:

I cannot change the Java code: System.getProperty("sdk.root");as this is coming from a compiled Arquillian container I can't modify.

我无法更改 Java 代码:System.getProperty("sdk.root");因为它来自已编译的 Arquillian 容器,我无法修改。

What I need would be a proper way of having this System.getPropertymethod to get the value.

我需要的是使用此System.getProperty方法获取值的正确方法。

Where to put this "sdk.root" for IntelliJ to pick it up? In a properties file or bash?

把这个“sdk.root”放在哪里让 IntelliJ 捡起来?在属性文件或 bash 中?

回答by laune

You have to use

你必须使用

String val = System.getenv( "PATH" );

for the value of an environment variable. A property value has nothing to do with the Environment of a process.

对于环境变量的值。属性值与进程的环境无关。

String val = System.getProperty( "sdk.root");

is for properties.

是为属性。

There should be a way of setting command line arguments in the dialog for preparing the execution of a Java program in your IDE. This is where you should define -Dsdk.root=....

应该有一种方法可以在对话框中设置命令行参数,以准备在 IDE 中执行 Java 程序。这是您应该定义 -Dsdk.root=... 的地方。