Trying to run a Java application on your Mac and need to set your JAVAHOME? Follow the instructions below to quickly and easily do it:. Open Terminal.
In this tutorial, we show you how to set $JAVA_HOME environment variable on latest or older Mac OSX. Mac OSX 10.5 or later In Mac OSX 10.5 or In Mac OSX 10.5 or later, Apple recommends to set the $JAVA_HOME variable to /usr/libexec/java_home, just export$JAVA_HOME in file. Mar 15, 2017 - [Dev] – How to set up “$JAVA_HOME” environment variable on Mac OS X? You can choose the version of JAVA via “/usr/libexec/java_home”.
Confirm you have JDK by typing “which java”. It should show something like /usr/bin/java. Check you have the needed version of Java, by typing “java -version”. JAVAHOME is essentially the full path of the directory that contains a sub-directory named bin which in turn contains the java. For Mac OSX – it is /Library/Java/Home.
Set JAVAHOME using this command in Terminal: export JAVAHOME=/Library/Java/Home. echo $JAVAHOME on Terminal to confirm the path. You should now be able to run your application Note that this sets JAVAHOME only for this session. If you want it to persist, you will have to add the command to your /.profile file.
Update: The link below does not have a complete answer. Having to set the path or variable in two places (one for GUI and one for shell) is lame. Not Duplicate of: Coming from a Windows background where it's very easy to set and modify environment variables (just go to System Properties Advanced Environment Variables), it does not seem to be that straight forward on Mac OS 10.5. Most references say I should update /etc/profile or /.profile. Are those the equivalent of System Variables and User Variables? For example, where should I set my JAVAHOME variable? EDIT: I want to be able to access the variable from the terminal as well as an app like Eclipse.
Also, I hope I don't have to restart/logout to make this take effect. There's no need for duplication. You can set environment variables used by launchd (and child processes, i.e.
Anything you start from Spotlight) using launchctl setenv. For example, if you want to mirror your current path in launchd after setting it up in.bashrc or wherever: PATH=whatever:you:want launchctl setenv PATH $PATH Environment variables are not automatically updated in running applications. You will need to relaunch applications to get the updated environment variables (although you can just set variables in your shell, e.g. PATH=whatever:you:want; there's no need to relaunch the terminal). There are several places where you can set environment variables. /.profile: use this for variables you want to set in all programs launched from the terminal (note that, unlike on Linux, all shells opened in Terminal.app are login shells).
/.bashrc: this is invoked for shells which are not login shells. Use this for aliases and other things which need to be redefined in subshells, not for environment variables that are inherited. /etc/profile: this is loaded before /.profile, but is otherwise equivalent. Use it when you want the variable to apply to terminal programs launched by all users on the machine (assuming they use bash).
![Mac Mac](/uploads/1/2/5/5/125519582/487952264.jpg)
![Environment Environment](/uploads/1/2/5/5/125519582/498300793.png)
/.MacOSX/environment.plist: this is read by loginwindow on login. It applies to all applications, including GUI ones, except those launched by Spotlight in 10.5 (not 10.6). It requires you to logout and login again for changes to take effect. This file is no longer supported as of OS X 10.8. your user's launchd instance: this applies to all programs launched by the user, GUI and CLI.
You can apply changes at any time by using the setenv command in launchctl. In theory, you should be able to put setenv commands in /.launchd.conf, and launchd would read them automatically when the user logs in, but in practice support for this file was never implemented. Instead, you can use another mechanism to execute a script at login, and have that script call launchctl to set up the launchd environment. /etc/launchd.conf: this is read by launchd when the system starts up and when a user logs in. They affect every single process on the system, because launchd is the root process. To apply changes to the running root launchd you can pipe the commands into sudo launchctl. The fundamental things to understand are:.
environment variables are inherited by a process's children at the time they are forked. the root process is a launchd instance, and there is also a separate launchd instance per user session. launchd allows you to change its current environment variables using launchctl; the updated variables are then inherited by all new processes it forks from then on. Example of setting an environment variable with launchd: echo setenv REPLACEWITHVAR REPLACEWITHVALUE launchctl Now, launch your GUI app that uses the variable, and voila! To work around the fact that /.launchd.conf does not work, you can put the following script in /Library/LaunchAgents/local.launchd.conf.plist:
Also, note that other programs that run at login may execute before the launchagent, and thus may not see the environment variables it sets. You can read up on linux, which is pretty close to what Mac OS X is. Or you can read up on BSD Unix, which is a little closer. For the most part, the differences between Linux and BSD don't amount to much. /etc/profile are system environment variables. /.profile are user-specific environment variables.
'where should I set my JAVAHOME variable?' . Do you have multiple users? Do they care? Would you mess some other user up by changing a /etc/profile? Generally, I prefer not to mess with system-wide settings even though I'm the only user.
I prefer to edit my local settings.