7

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?

Bogdan
  • 2,608
  • 2
  • 27
  • 26
  • Have a look at http://stackoverflow.com/questions/603785/environment-variables-in-mac-os-x?lq=1 this question was already asked (not related to Java). – Bastien Jansen Jan 28 '13 at 12:41
  • What are you trying to accomplish? It should very very rarely be necessary to mess with the global environment - provide a suitable launcher script instead. Using `java -D` won't affect the environment, you retrieve those values using `System.getProperty()`. (It's also probably more idiomatic to configure a Java program with those.) – millimoose Jan 28 '13 at 23:16
  • You have to log in and log out for `environment.plist` changes to apply. – millimoose Jan 28 '13 at 23:18
  • Hi! Thanks for the tips. I manage to fix it by adding the .MacOSX/environment.plist in my user folder. I din't know that '~' points to my user's folder... and of course logout/login. I know this is a stupid thing to set, but I need to use some java apis that read some variables using System.getenv, so ... – Bogdan Feb 08 '13 at 09:20

2 Answers2

9

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:

  1. exit Eclipse,
  2. run export VAR_NAME=Value in a command shell instance, and
  3. 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?

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • [The docs for `environment.plist`](https://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPRuntimeConfig/Articles/EnvironmentVars.html) state it's applied by the OS X login process. – millimoose Jan 28 '13 at 23:18
  • It worked only creating .MacOSX/environment.plist in my user's folder and logout/login (or restart). Nothing else worked ... strange. – Bogdan Feb 08 '13 at 09:23
0

1.open the Terminal

2.export VAR_NAME=Value

3.open /Applications/Eclipse.app