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?
/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.
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.
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:
export VAR_NAME=Value
in a command shell instance, andRe the things you tried.
I used this command in the terminal
export VAR_NAME=Value
butSystem.getEnv(VAR_NAME)
returnsnull
.
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:
-DVAR_NAME=...
is setting a system property not an environment variable, andjava
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With