11

According to this mobilefirst tutorial, it mentioned:

You must have the JAVA_HOME environment variable set to your JDK directory.

For example:

Mac OSX: /Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home

I've added this 2 lines in .bash_profile:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
export PATH=$PATH:$JAVA_HOME/Contents/Commands

Is this correct?

Thanks.

Jerry Chong
  • 7,954
  • 4
  • 45
  • 40
user1872384
  • 6,886
  • 11
  • 61
  • 103
  • 1
    Did you follow the steps?: Download (1) and install (4) the packages? Especially in (5) "_log out from the OS, and then log back in_" – superbob Sep 11 '15 at 07:29
  • miss out number 5 =.='' thank you very much – user1872384 Sep 11 '15 at 07:33
  • Possible duplicate of [Where is JAVA\_HOME on macOS Mojave (10.14) to Lion (10.7)?](https://stackoverflow.com/questions/6588390/where-is-java-home-on-macos-mojave-10-14-to-lion-10-7) – Martin Choraine Oct 09 '19 at 15:16
  • $ vim .bash_profile export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home $ source .bash_profile $ echo $JAVA_HOME /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home – Shomu Dec 03 '19 at 06:51

4 Answers4

15

in .bash_profile:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.xx/Contents/Home
Peter Salomonsen
  • 5,525
  • 2
  • 24
  • 38
  • Hey @Peter Salomonsen. I've set it to that directory and installed the CLI program. However when I use the terminal to create a new project with this command: "mfp create helloworld" it still gives me this error : -bash: mfp: command not found. Can you please help? – user1872384 Sep 11 '15 at 07:12
  • @user1872384 Then that means that the `mfp` command was not found in the `$PATH`. That doesn't necessarily have anything to do with `$JAVA_HOME`. – 200_success Sep 11 '15 at 07:13
  • I see.. Do you have any idea how to fix that? – user1872384 Sep 11 '15 at 07:22
  • Got it just need to logout and login after installation :D – user1872384 Sep 11 '15 at 07:32
  • 1
    need to save .bash_profile **source .bash_profile** – Shomu Dec 03 '19 at 06:53
10

Adding the below answer to help those who are looking for step by step instructions on how to setup Java_Home on a Mac.

  1. Determine whether Java is installed by using the command below:

    which java
    

You will something like this - /usr/bin/java

  1. Next Step will be to determine the version of Java Installed by using the command below:

    java -version
    

You will see something like java version "1.8.0_131"

  1. Next step will be to get the location where the Java is installed:

    cd /Library/Java
    
  2. Under the Java folder, select the folder with the version that was displayed earlier:

    /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
    
  3. Next check if Java_Home is setup to the correct location:

    echo $JAVA_HOME
    

It will result in blank output if it is not already setup. If it returns the location, check if it points to the correct folder.

  1. You can add or update the Java_Home using the below commands:

    vi ~/.bash_profile
    

Navigate to the end of the file by pressing "Shift + g". Now press "i" to get to insert mode. Add the below lines in the bash_profile after replacing the path to the java home directory on your mac

    # Setting Java_Home
    export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home

Save this by pressing Esc followed by ":wq!"

  1. Finally open a new terminal window and test the variable is setup correctly:

    echo $JAVA_HOME
    

It should return the Java_Home path setup

4

If your default terminal is /bin/zsh (Z Shell) like in my case, then you should set these environment variable in ~/.zshenv file with following contents:

export JAVA_HOME="$(/usr/libexec/java_home)"

Similarly, any other terminal type not mentioned above, you should set environment variable in its respective terminal env file.

After saved the content into env file, restart terminal and call following commands:

echo $JAVA_HOME

It should display the full Java path.

Jerry Chong
  • 7,954
  • 4
  • 45
  • 40
1

Just a note to say this still works in OpenJDK version 11. As shown in the original question the java_home utility lives in /usr/libexec, which might not be on your $PATH. You can run the command directly in a terminal window, here's sample output too:

$ /usr/libexec/java_home -v 11
/Library/Java/JavaVirtualMachines/jdk-11.0.4.jdk/Contents/Home

And set JAVA_HOME to that value. Even better, wire your shell script's dot files as suggested in the original question by @user1872384, because that way your environment automatically keeps up with JDK upgrades as you install them.

chrisinmtown
  • 3,571
  • 3
  • 34
  • 43