4

I am working on a ubuntu 14.0.4 machine.

I exported a variable TEST_HOME in my .bashrc file using

export TEST_HOME=/home/dev/code/test

When I tried echo $TEST_HOME from terminal, it returned /home/dev/code/test

So far, so good.

When I try from Java code :

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

value is null.

Am I missing something here?

Community
  • 1
  • 1
Dev
  • 13,492
  • 19
  • 81
  • 174
  • 1
    For that variable to be in Java's environment, you will have to start the Java process from a shell where you have that variable set. Did you? – folkol Mar 29 '16 at 07:19
  • @folkol I did not export this variable in a particular shell/terminal. I added it's entry in `.bashrc` – Dev Mar 29 '16 at 07:21
  • close and reopen your terminal to refresh .bashrc defined changes – vikingsteve Mar 29 '16 at 07:23
  • This [link](http://unix.stackexchange.com/questions/117467/how-to-permanently-set-environmental-variables) might help you. – ujulu Mar 29 '16 at 08:45
  • @vikingsteve I am not able to get this value from Java code. I am able to get it's value from terminal. – Dev Mar 30 '16 at 06:39
  • 1) Kill java process 2) Restart session 3) Try it again – Héctor Mar 30 '16 at 06:48
  • @bigdestroyer I am writing java code in Eclipse IDE. I killed eclispe instance, restarted it again it's null. – Dev Mar 30 '16 at 06:50
  • Try killing java from process manager – Héctor Mar 30 '16 at 06:52

2 Answers2

10

Mentioning the variable in .bashrc will work only for programs started from shell. For system wide environment variables mention it in /etc/environment.

Refer Ubuntu Environment variables

Rima
  • 545
  • 6
  • 12
  • currently only path variable is there in my `/etc/environment`. Do I need to `export TEST_HOME=/home/dev/code/test` in `/etc/environment`? – Dev Mar 30 '16 at 07:13
  • I added `export TEST_HOME=/home/dev/code/test` in `/etc/environment`. Now `echo $TEST_HOME` is returning nothing (I restarted terminal), same null from Java code (I restarted eclipse) Do I need to restart machine for this? – Dev Mar 30 '16 at 07:27
  • This [Reload env](http://superuser.com/questions/339617/how-to-reload-etc-environment-without-rebooting) will help, to avoid restarting machine. – Rima Mar 30 '16 at 08:55
1

.bashrc would set environment variable only for bash shell. To set it system wide set it in /etc/environment file.

Since you are using eclipse, and it does not run within bash shell, it is not getting the variable you are setting. If you run your programme using java command line in your terminal then it should get it.

Setting variable in /etc/environment would make it available to eclipse. You will need to restart your machine once you update /etc/environment.

Sanket Meghani
  • 885
  • 2
  • 14
  • 22