How to set java environment variable on mac
environment-variables, java, macos
Solution
It is not clear what you are actually doing here. If you are trying to set an environment variable for a Java command you are launching from Eclipse, then the simple solution is to set it via the Eclipse command launcher configuration. Another alternative is to:
- exit Eclipse,
- run `export VAR_NAME=Value` in a command shell instance, and
- launch Eclipse from that same shell instance.
Re the things you tried.
I used this command in the terminal `export VAR_NAME=Value` but `System.getEnv(VAR_NAME)` returns `null`.
If you run the `export` command from a command prompt, and then immediately launch the command from the same command prompt, that should work. The export command is telling the shell instance used by the command prompt to add `VAR_NAME` to this list of environment variables it exports to child processes that started after running the `export` command. Child processes that have all ready been started won't see the changes.
I strongly suspect that you ran the `export` after you launched Eclipse ... or that you didn't start Eclipse from that shell instance. If either of those two is true, the `export` command wouldn't affect Eclipse's environment variables which it (by default) passes on to any Java program you launch from Eclipse.
I tried `java -DVAR_NAME=Value` but nothing is executed. I only get the java arguments help.
That fails for a couple of reasons:
- the `-DVAR_NAME=...` is setting a system property not an environment variable, and
- you haven't told `java` the name of the class that you want to start!
I created in my current project folder the file `.bash_profile` with the content: `export VAR_NAME=Value`
That only affects new shell instances that are launched after you created the file. It doesn't affect the existing one.
I also created the folder .MacOSX with a single file inside environment.plist and in this file I wrote the property and its value using XCode.
I've no idea what that would do. Where did you create that folder?
Problem
How do I set a java environment variable that can be read with `System.getenv()` on Mac OS X Lion? I developing in Eclipse. I used this command in the terminal `export VAR_NAME=Value` but `System.getEnv(VAR_NAME)` returns `null`. I tried `java -DVAR_NAME=Value` but nothing is executed. I only get the java arguments help. I created in my current project folder the file `.bash_profile` with the content: `export VAR_NAME=Value`. It's not working. I also created the folder `.MacOSX` with a single file inside `environment.plist` and in this file I wrote the property and its value using XCode. Nothing is working. How do I do it?