Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set java environment variable on mac

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?

like image 239
Bogdan Avatar asked Jan 28 '13 12:01

Bogdan


People also ask

Where is the JAVA_HOME variable set in Mac?

/usr/libexec/java_home is the simplest maintainable way of setting JAVA_HOME on macOS. You can use java_home to: Find all installed JDKs. this works for me on a 2019 Mac running Catalina in 2020.

How do I set environment variables in Terminal Mac?

It depends on the case, if you need a variable for just one time, you can set it up using terminal. Otherwise, you can have it permanently in Bash Shell Startup Script with “Export” command. And then close the terminal window and open another one to check out if the set variable has disappeared or not.


1 Answers

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?

like image 91
Stephen C Avatar answered Sep 19 '22 03:09

Stephen C