Readers from java using Eclipse

Wherever I look, it says that you can get the environment variable using System.getenv(str).

This does not work for me. Here's what I do: OS: Mac OS x 10.7 Java 1.6.x

If I do export abc=/hello/ in my terminal and then echo $abc , it gives me a variable. If I close the terminal, open it again and make echo $abc , it disappeared. To overcome this, I edited the .bash_profile file and inserted export abc=/hello/ . Close the terminal, do echo $abc and it will work. Therefore, I realized that the env variable is constant.

Now, if in my java console application, I type System.getenv("abc") , it returns null . What am I missing?

+4
source share
2 answers

The reason you need to put export in your .bash_profile is because the environment variables in the shell save only the variables in that shell and - since you used export - for the children of this shell, or, in other words, other programs running this shell.

If you run Java code from Eclipse and run Eclipse from a shell with the environment variables you set, your program should see additional environment variables. To start Eclipse from the shell, you need the OS X open command:

 $ open /Applications/eclipse/Eclipse.app 

Alternatively, you can set environment variables in an Eclipse project, and you will need to do this if you are not running Eclipse from a shell with the proper environment. In the Run Configurations dialog box, find the Environment tab. Here you will find a table for adding environment variables that will be passed to your program.

It’s better to add environment variables to your startup configuration, as they will always be available for your project. Your code doesn’t really care where the environment variables come from, and adding them to the project is simpler and will work the same on different platforms.

Of course, when you run your program outside of Eclipse, you need to make sure that the same environment variables exist in the shell where you, for example. execute java .

+7
source

Eclipse does not use system env variables unless you start directly from the shell (it usually starts by clicking on its icon). In this case, you will need to explicitly specify the required env variables in the environment tab for the configuration of the program launch.

+3
source

Source: https://habr.com/ru/post/1434063/


All Articles