简体   繁体   English

从Java读取时Linux / bash环境变量为null

[英]Linux/bash environment variable is null when reading from java

In Ubuntu I have exported the below environment variable in ~/.bashrc : 在Ubuntu中,我在〜/ .bashrc中导出了以下环境变量:

export MY_VAR="MY_VAR_VALUE"

When I re-open a terminal I can do: 重新打开终端时,我可以执行以下操作:

~ $ echo $MY_VAR
MY_VAR_VALUE
~ $ 

But when I do this in Java/Eclipse (which I have restarted after exporting the test env. var): 但是,当我在Java / Eclipse(在导出测试环境变量var之后重新启动)中执行此操作时:

String myVar = System.getenv("MY_VAR");
System.out.println(myVar);

I just get null . 我只是 Does eclipse not see variables set in ~/.bashrc ? eclipse是否看不到〜/ .bashrc中设置的变量?

In Linux (unlike Windows) you need a direct child relatinship between the Java executable, started by Eclipse, started by the Shell where you have exported the variable. 在Linux(与Windows不同)中,您需要在Java可执行文件(由Eclipse启动,由导出变量的Shell启动)之间直接进行子关系。

So you must start Eclipse from within that Shell. 因此,您必须从该Shell中启动Eclipse。 It wont work if you doubleclick on the executable in desktop or menu. 如果双击桌面或菜单中的可执行文件,它将无法正常工作。

But I would specify the environment variables you need in the run configuration and not rely on such a setup. 但是我会在运行配置中指定所需的环境变量,而不依赖于这样的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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