37
votes

Pretty new to Java and also to Mac ... I want to make sure JAVA_HOME is set so in other programs I can use its path. So I did some Googling and here is what I got:

If I enter /usr/libexec/java_home in terminal I get this: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home but if I enter echo $JAVA_HOME in terminal, I don't get anything back.

Can you please tell me what is going on in here?

Thanks.

6
export JAVA_HOME=$PATH:/usr/libexec/java_home worked for meLucky Rana

6 Answers

75
votes

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.

15
votes

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

6
votes

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
1
votes

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

1
votes

This is not resilient to new installs of the JDK. According to Apple: - you should not set it globally - you should use libexec

http://lists.apple.com/archives/java-dev/2011/May/msg00040.html

You might think that:

$ cat /etc/launchd.conf setenv JAVA_HOME /usr/libexec/java_home

would work - but no, it sets it literally.

But, as I say, according to that Apple Engineer, you are not supposed to set it globally anyway. :(

1
votes

I had this problem after setting JAVA_HOME with jenv. You can solve the problem by editing file

/Applications/Eclipse.app/Contents/Info.plist

Just set path to your version of java by uncommenting / editing appropriate part of file. For AdoptOpenJDK installed with homebrew it looks like this:

<key>Eclipse</key>
<array>
    <string>-vm</string><string>/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home/bin/java</string>
    <!-- to use a specific Java version (instead of the platform's default) uncomment one of the following options,
                   or add a VM found via $/usr/libexec/java_home -V
        <string>-vm</string><string>/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Commands/java</string>
        <string>-vm</string><string>/Library/Java/JavaVirtualMachines/1.8.0.jdk/Contents/Home/bin/java</string>
    -->
        <string>-keyring</string>
    <string>~/.eclipse_keyring</string>
</array>