37

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.

Bohn
  • 26,091
  • 61
  • 167
  • 254

6 Answers6

77

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.

Mat
  • 202,337
  • 40
  • 393
  • 406
Steve McGuire
  • 1,467
  • 12
  • 8
  • thanks, didn't know these. Ok how can I navigate to this place that you mentioned? "~/.profile" – Bohn Jul 07 '12 at 04:14
  • 4
    I edited the answer so you can just execute those two commands and be done. For future reference, you could have done: vim ~/.profile, then press i to go into edit mode, paste in the code, then escape, press :wq, done. Vim is a good tool to learn as well :) – Steve McGuire Jul 07 '12 at 04:16
  • 5
    umm, you probably want >> instead of > in case ~/.profile already exists. – Matt Jul 07 '12 at 06:21
  • Years pass. Question: do you want " (double) quotes or ' (single) quotes around the export command? Using " quotes means we execute the _java_home_ command as part of the echo, hard-coding the resulting path. Using ' (single) quotes would execute _java_home_ every time you log in, which might be what you really want. Up to you, but I'd go for single quotes: if the path was that well defined, they wouldn't need a command to generate it! OTOH, maybe you don't want JAVA_HOME swinging around wildly whenever you log in. – SusanW Jun 17 '16 at 09:43
  • @shovavnik also running a hot reload of `~/.bash_profile` helped me further along by doing - `source ~/.bash_profile` – BradGreens Nov 22 '16 at 22:29
15

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

Lone Ronin
  • 2,530
  • 1
  • 19
  • 31
WebEpic
  • 697
  • 6
  • 5
6

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
Atharva
  • 6,711
  • 5
  • 31
  • 39
  • 1
    ok thanks, one stupid question: what is this "~" I see in so many places? – Bohn Jul 07 '12 at 04:23
  • 1
    It's just an alias for the home folder of the current user. That home folder on osx would be located under /Users – Steve McGuire Jul 07 '12 at 04:24
  • oh ok ok, Then how can I find out what is the full path to my home folder? – Bohn Jul 07 '12 at 04:25
  • You can just do: echo $(dirname ~/)/$(basename ~/) – Steve McGuire Aug 10 '12 at 00:41
  • 1
    Please update this to read `export PATH=$PATH:$JAVA_HOME/bin` instead of `export PATH=PATH:$JAVA_HOME/bin` because without the "$" it wipes out the user's path so that commands like `ls` and `nano` don't work. Many users won't understand to use `/usr/bin/nano` so they will be stuck without a working shell. And worse, it even affects newly opened shell windows in Terminal.app, as well as `sudo`, and can be hard to recover from. But your answer was helpful for `appium-doctor`, because Appium didn't mention how to fix "WARN AppiumDoctor ✖ Bin directory for $JAVA_HOME is not set" in their docs – Zack Morris Feb 07 '17 at 23:02
1

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

Hashken
  • 4,396
  • 7
  • 35
  • 51
  • well I copy-pasted /etc/launchd.conf in terminal, but it says no such file or directory exists – Bohn Jul 07 '12 at 04:16
  • From what I understood, after a bit of googling, /etc/launched.conf does not exist by default, so just create it. – Hashken Jul 07 '12 at 04:23
  • Take a look at the updated answer. The given command should be able to create the file. Note that, making changes in /etc/launchd.conf sets JAVA_HOME for all users. – Hashken Jul 07 '12 at 04:30
1

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. :(

Martin Cleaver
  • 909
  • 8
  • 21
  • 1
    Just in case anyone else finds this. It does work, but the grave characters are important. \`/usr/libexec/java_home\` They are eaten by the formatting here on SO. – Danny Parker Aug 13 '15 at 15:53
1

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>
swch
  • 1,432
  • 4
  • 21
  • 37