Friday, January 12, 2024

How to set JAVA_HOME (PATH) in Mac OS? Example Tutorial

You can set JAVA_HOME in Mac OS X 10,10 or Yosemite by adding the following command in your ~/.bash_profile file, as shown below:

export JAVA_HOME=`/usr/libexec/java_home` (remember backticks)
or
echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.7)" >> ~/.bash_profile

This will append export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.7)" into your bash_profile file. If you have set JAVA_HOME in UNIX then it's exactly similar to that.

Apple's Mac OS X is the second most widely used operating system after Windows and no surprise that many Java programmers use Macbook Pro for Java development. Mac's UNIX-like feature is added advantage because most of the real-world Java servers run on UNIX-based systems e.g. Linux or Solaris. In order to run Java, two things are most important, PATH and CLASSPATH.

In order to use Java from the command line, you also need to define JAVA_HOME or JRE_HOME, many Java-based programs and tools use these environment variables to access Java e.g. Maven, Tomcat, or Eclipse. Since Java is an optional package on the latest version of OS X, starting from OSX 10.7 (Lion), you need to either install Oracle JDK or choose this optional package.




In this article, you will learn how to set the JAVA_HOME environment variable in different Mac OS X versions e.g.

Mac OS X 10.7 Lion
Mac OS X 10.8 Mountain Lion
Mac OS X 10.9 Mavericks
Mac OS X 10.10 Yosemite
Mac OS X 10.11 El Capitan (Beta)

Actually, the steps are exactly the same (if you are running on a Mac version greater than 10.6), you don't need to do anything special depending upon your version e..g Mavericks or Yosemite. You can follow the same steps to set the JAVA_HOME environment variable

Just remember to use option -v to set JAVA_HOME to a particular version of JDB, of course, it's only useful if you have multiple JDK installed on your MacBook Pro.

How to set JAVA_HOME in Mac OS X Yosemite


How to set JAVA_HOME to JDK 1.7 in MAC OS X 10.10 Yosemite

If you have installed Oracle JDK 1.6 then you can follow these steps to point the JAVA_HOME environment variable to that JDK:

1) Open a terminal window

2) Open ~/.bash_profile file and add the below line:

export JAVA_HOME="$(/usr/libexec/java_home -v 1.7)"
or

export JAVA_HOME=`/usr/libexec/java_home -v 1.7`


3) Close the terminal and open a new one, so that your JAVA_HOME change takes effect. Alternative you can also do

$ source ~/.bash_profile 

to import new environment variables.

Similarly, if you want to set your JAVA_HOME to point to JDK 8 you can add the following lines to your bash_profile file:

export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)" 
or
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

In short, you can specify the JDK version using the -v option of java_home, of course, you need to download and install the corresponding JDK version.


That's all about how to set the JAVA_HOME environment variable in the Mac OS X system. From version 10.6 onward, it's recommended to set JAVA_HOME variable using /usr/libexec/java)home and not using something like /System/Library/Frameworks/JavaVM.frameworks/Version/1.6.0_22/Home, which is subject to change from release to release. In order to set PATH just add this JAVA_HOME/bin into PATH.

These steps work in OS X version Lion, Mountain Lion, Mavericks, Yosemite, and even in the beta version of the latest Mac version OS X El Capitan. Don't forget to set JAVA_HOME using option -v if you have multiple Java versions installed in your Macbook.

 You can also see man java_home for further details. Let us know if you face any problems while using Java in Apple's Macbook Pro and we'll try to help.

6 comments:

  1. Hi All,

    Good day .

    Can anyone here tell me how to set java classpath for JAVA 8 in mac os x 10.11 version

    Thanks!

    ReplyDelete
  2. Can anyone tell me java class path for new version java 9 in mac version.

    ReplyDelete
  3. lass Result
    {

    /*
    * Complete the 'pickingNumbers' function below.
    *
    * The function is expected to return an INTEGER.
    * The function accepts INTEGER_ARRAY a as parameter.
    */

    public static int pickingNumbers(List a)
    {
    // Write your code here
    int max=0;
    int count=0;
    Collections.sort(a);
    for(int i=1;imax)
    {
    max=count;
    }
    }
    }

    }
    return max;
    }





    error:Solution.java:48: error: illegal start of type
    return max;
    ^
    Solution.java:48: error: ';' expected
    return max;
    ^
    2 errors
    Solution.java:48: error: illegal start of type
    return max;
    ^
    Solution.java:48: error: ';' expected

    ^
    2 errors
    how to fix those errors

    ReplyDelete
  4. Is it required for the value object in HashMap to implement hashCode and equeals method??

    ReplyDelete
    Replies
    1. No, not necessary, it's only mandatory for key object, see my post How HashMap works in Java to learn more

      Delete

Feel free to comment, ask questions if you have any doubt.