Monday 28 July 2014

how to setup up JAVA_HOME environment variable on mac

How to setup up JAVA_HOME environment variable on mac laptop

Being as a first point
Open Terminal from your mac this is command prompt on mac laptop.
Now type echo $JAVA_HOME it should return java details if it is already installed.
So that you will come to know. whether it is existed or not.

In Mac laptop all environment variables will setup in bash_profile

So if you are first time setting up in the scene it might not exist. So to let you know whether it is existed or not Type below command  on Terminal command prompt.

ls -1 ~/.bash_profile

If it is not existed then it will say “No such file or directory”

Now type this command
touch ~/.bash_profile

Now open file in vim editor this is editor in unix.
vim ~/.bash_profile
it will open some text editor.
If you try to type any thing on this.. it wont allow.. if you want to insert any thing please press
esc + i   it allow to enter data into vim editor

Now enter these lines on that editor

export JAVA_HOME=/Library/Java/Home
export JRE_HOME=/Library/Java/Home

Now you have to save and exit from the editor to do that please press escape : colon then wq
6. esc + :wq
To execute that file please enter this command.
7. source ~/.bash_profile
To check java details please enter this command
8. echo $JAVA_HOME
9.echo $JRE_HOME

Any how please have some help on this video
JAVA_HOME environment setup help video on mac



To make sure those environment variables work in all sessions.
We have to do as below.

type env | grep M2_HOME on the terminal that is working. This should give something like
M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1

typing env | grep JAVA_HOME should give like this:
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home
Now you have the PATH for M2_HOME and JAVA_HOME.


now open .bash_profile on Terminal  if you don't have one just create bash_profile
vi ~/.bash_profile
Add the following:

#set JAVA_HOME

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home

export JAVA_HOME

M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1

export M2_HOME

PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin

export PATH

After this click on esc button and : and wq so that your file will save.

Now type

source ~/.bash_profile   click enter

This steps executes the commands in the .bash_profile file and you are good to go now.

Now type mvn --version

No comments:

Post a Comment