简体   繁体   English

System.getenv(“ TEST_HOME”)返回null

[英]System.getenv(“TEST_HOME”) is returning null

I am working on a ubuntu 14.0.4 machine. 我正在使用Ubuntu 14.0.4机器。

I exported a variable TEST_HOME in my .bashrc file using 我使用以下命令在我的.bashrc文件中导出了变量TEST_HOME

export TEST_HOME=/home/dev/code/test

When I tried echo $TEST_HOME from terminal, it returned /home/dev/code/test 当我尝试从终端echo $TEST_HOME时,它返回了/home/dev/code/test

So far, so good. 到现在为止还挺好。

When I try from Java code : 当我尝试使用Java代码时:

String value = System.getenv("TEST_HOME");

value is null. value是null。

Am I missing something here? 我在这里想念什么吗?

Mentioning the variable in .bashrc will work only for programs started from shell. .bashrc提及变量仅对从shell启动的程序有效。 For system wide environment variables mention it in /etc/environment . 对于系统范围的环境变量,请在/etc/environment提及它。

Refer Ubuntu Environment variables 引用Ubuntu环境变量

.bashrc would set environment variable only for bash shell. .bashrc只会为bash shell设置环境变量。 To set it system wide set it in /etc/environment file. 要在系统范围内进行设置,请在/etc/environment文件中进行设置。

Since you are using eclipse, and it does not run within bash shell, it is not getting the variable you are setting. 由于您使用的是eclipse,并且它无法在bash shell中运行,因此它不会获取您正在设置的变量。 If you run your programme using java command line in your terminal then it should get it. 如果您在终端中使用java命令行运行程序,则应该可以得到它。

Setting variable in /etc/environment would make it available to eclipse. /etc/environment设置变量将使其可用于Eclipse。 You will need to restart your machine once you update /etc/environment . 更新/etc/environment您将需要重新启动计算机。

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

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