5

Everywhere I search, it says you can get an environment variable by using System.getenv(str).

It's not working for me. Here's what I am doing: 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 the variable. If I close the terminal, reopen it again and do echo $abc, it's gone. To overcome this, I edited my .bash_profile file and inserted export abc=/hello/. Close the terminal, do echo $abc and it works. So I understood that the env variable is permanent now.

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

2
  • 2
    how are you launching your java app? Commented Sep 13, 2012 at 20:14
  • the session that you are running on doesn't have the new exports in .bash_profile, re-exec .bash_profile from your current session and all applications should be able to see it then. Commented Sep 13, 2012 at 20:20

3 Answers 3

8

The reason that you needed to put the export in your .bash_profile is that setting environment variables in a shell only persist the variables in that shell, and - since you used export - to children of that shell, or in other words, other programs launched by that shell.

If you're running your java code from Eclipse, and you launch Eclipse from a shell with your environment variables set, then your program should see the added environment variables. To launch Eclipse from the shell, you'll need to use the OS X open command:

$ open /Applications/eclipse/Eclipse.app

Alternately, you can set the environment variables within your Eclipse project, and you'll need to do this if you're not launching Eclipse from a shell with the proper environment. In the Run Configurations dialog, look for a tab named Environment. Here you'll find a table for adding environment variables that will be passed to your program.

It's better to add the environment variables to the Run Configuration since that way they'll always be available to your project. Your code doesn't actually care where the environment variables are coming from, and adding them to the project is simpler, and will work the same way on different platforms.

Of course, when you run your program outside Eclipse, you'll need to make sure that the same environment variables exist in the shell where you e.g. run java.

Sign up to request clarification or add additional context in comments.

5 Comments

And if I run my app from the terminal, it will get the env variable I set in the bash_profile file? No need for Run configurations then?
@Salmanmahmood yes, if you run Eclipse from the terminal, you'll need to use the OS X open command, but then you should see the same env vars. I'll add to the answer
Thanks!! one last query. if i set the env variable in terminal, compile my *.java file using javac in terminal, then run the app in terminal using java command, will it pick the env variables? (without writing to .bash_profile)
any environment variables that exist in the shell when you run java will be available to your program. So if you set it in the shell before running your program, or set it in your .bash_profile, then run your program outside Eclipse, then you'll see the same environment variables
what should be done to get the environment variables in eclipse even if its not launched from shell?
3

Eclipse does not use the system's env variables unless launched directly from the shell (which is how it is generally launched, by clicking its icon). In that case you will have to explicitly set the required env variables in the environment tab of the run configuration of the program.

3 Comments

On the contrary: Eclipse will pick up system environment variables, including any environment variables set in the shell from which Eclipse is launched on the command line
@pb2q made it explicit in the answer.
well, the answer is still misleading. Eclipse picks up env variables like any other program, which means any system environment variables set e.g. for the System or User in Windows, or shell environment vars in UN*X systems.
0

I too faced The same issue , I resolved it this way.

  • Open Terminal

  • cd to the folder where eclipse.app is located E.g cd /Users/Shared/eclipse/jee-2020-09

  • Type open Eclipse.app/

  • Eclipse will now open and will be able to access the system environment variables as well.

Check it using the code:

System.getenv().forEach((k, v) -> {
    System.out.println("ENV : " + k + ":" + v);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.