Do you have questions ? We got the answers!

Ask a question:

0
How to Set $JAVA_HOME environment variable on Mac OS X

Solved 1 Answers 62 Views Others
How to Set $JAVA_HOME environment variable on Mac OS X

1 Answer

0
Best answer

1. Mac OSX 10.5 or later

In Mac OSX 10.5 or later, Apple recommends to set the $JAVA_HOME variable to /usr/libexec/java_home, just export$JAVA_HOME in file ~/. bash_profile or ~/.profile.

Bash

$ vim .bash_profile 

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

$ source .bash_profile

$ echo $JAVA_HOME

/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home

Why /usr/libexec/java_home?

This java_home can return the Java version specified in Java Preferences for the current user. For examples,

Bash

/usr/libexec/java_home -V

Matching Java Virtual Machines (3):

    1.7.0_05, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home

    1.6.0_41-b02-445, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

    1.6.0_41-b02-445, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

This Mac OSX has three JDK installed.

Bash

##return top Java version

$ /usr/libexec/java_home

/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home

## I want Java version 1.6

$ /usr/libexec/java_home -v 1.6

/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

2. Older Mac OSX
For older Mac OSX, the /usr/libexec/java_home doesn’t exists, so, you should set JAVA_HOME to the fixed path :

Bash
$ 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

answered Feb 10, 2016 by anonymous
selected Mar 7 by stbadmin

Top Contributors

  • 4970 pointsstbadmin

    34 questions 454 answers218 comments

  • 433 pointsSunilBhaskar

    41 questions 15 answers32 comments

  • 425 pointsMehedi

    36 questions 3 answers14 comments

  • 364 pointssumitkumarwatts

    0 questions 14 answers0 comments

  • 354 pointsSoft Tester

    43 questions 0 answers4 comments

  • 327 pointsKeeshaGolden

    0 questions 17 answers2 comments

  • 322 pointsVipul Sharma

    44 questions 0 answers24 comments

  • 310 pointsvysh.sharma

    25 questions 0 answers0 comments

  • 267 pointsOnkar_Nimje

    33 questions 15 answers15 comments

  • 258 pointsVanitha

    26 questions 0 answers6 comments

...