Making sure JAVA_HOME is correctly set

Checking JAVA_HOME path

Try running source .bash_profile prior to echo $JAVA_HOME in your root directory. This should correct the problem if you've set JAVA_HOME correctly. If you're not sure you're in your root directory, simply type cd ~, press enter and you're there.

Root Directory

  • Explanation: source loads and runs your bash_profile.

If you haven't set JAVA_HOME correctly, following the instructions below should clear things up.

  • vim .bash_profileopens your bash_profile in Vim.
    • I've included a list of VIM commands you'll likely need to edit your .bash_profile below.

  • export JAVA_HOME=$(/usr/libexec/java_home)creates an ENV_VAR (Environment Variable) and sets/stores the home path of the JDK to (/usr/libexec/java_home).
  • Exit vim and type the following at the terminal*
  • source .bash_profileloads and runs your updated bash_profile
  • echo $JAVA_HOMEreturns the value stored in the ENV_VAR JAVA_HOME, which is the home path of your JDK installation.

VIM Commands:

Vim is an editor to create or edit a text file. There are two modes in vim.

  • Command Mode: user can move around the file, delete text, etc.

  • Insert Mode: user can insert text.

Changing between modes:

Command mode to Insert mode

  • type the appropriate letter for the action you want (a, A, i, I, o, O) -- details for letters below.

Insert mode to Command mode

  • press Esc (escape key)

Text Entry Commands (Used to start text entry)

  • a -- Append text following current cursor position

  • A -- Append text to the end of current line

  • i -- Insert text before the current cursor position

  • I -- Insert text at the beginning of the cursor line

  • o -- Open up a new line following the current line and add text there

  • O -- Open up a new line in front of the current line and add text there

Cursor Movement Commands (only used in the commands mode.)

  • h -- Moves the cursor one character to the left

  • l -- Moves the cursor one character to the right

  • k -- Moves the cursor up one line

  • j -- Moves the cursor down one line

  • nG or :n -- Cursor goes to the specified (n) line

  • (ex. 10G goes to line 10)

  • $ -- Move cursor to the end of current line

  • 0 -- (zero) Move cursor to the beginning of current line

  • w -- Forward one word

  • b -- Backward one word

Exit Commands

  • :wq -- Write file to disk and quit the editor

  • :q! -- Quit (no warning)

  • :q -- Quit (a warning is printed if a modified file has not been saved)

  • ZZ -- Save workspace and quit the editor (same as :wq)

VIM Editor Commands -- full list

osxterminaljava


JAVA_HOME isn't set by default on OSX. You can resolve this by opening terminal and executing the following:

echo "export JAVA_HOME=`/usr/libexec/java_home`" >> ~/.profile    
. ~/.profile

This will cause JAVA_HOME to be set on startup (rather than just the current session), and immediately add it.


The empty value of the echo command would mean that the value has not been set correctly as you are expecting. You can try creating/editing ~/.profile by adding the lines something like:

vi ~/.profile
export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home  

if you are not yet comfortable with vi editor, you may also create that file in your home directory using TextEdit program in Mac. and execute

source ~/.profile

That will load set the JAVA_HOME property. Next you can try out executing echo $JAVA_HOME to check whether it has been set correctly as you expect.

You may also need to set PATH environment variable as something like:

export PATH=PATH:$JAVA_HOME/bin

Edit your /etc/launchd.conf in your text editor. If it does not exist create it.

Then append the following line to the file: setenv JAVA_HOME /path/to/java/installation

OR

just type the following command in terminal
sudo echo setenv JAVA_HOME /path/to/java/installation >> /etc/launchd.conf Then just enter your password when it prompts.

Now reboot your computer and the changes should have taken place.

Note :
I am giving you advice based on my experience in Linux. But, these should work on MAC also.

Source

Tags:

Java

Macos