Java -version reporting wrong version on Mac OSX Mountain Lion

Solution 1:

Ok, thanks to MBraedley I found the solution.

Quoting from the JRE 7 Installation Guide for Mac OS X:

Installing a JRE from Oracle will not:

  • Update java -version symlinks or add java to your path.
  • Show the installed JRE in the Java Preferences.app

To be able to do the above, you need to install the JDK.

Solution 2:

put this in your start up script (.profile, .bash_profile etc):

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

It's possible you are setting JAVA_HOME to the wrong version. Before Java 7, I had

export JAVA_HOME=/Library/Java/Home/

and I was still getting Java 6.

Solution 3:

Yes Sometimes this happen as your machine not reflect the correct version. First find out where do you store the environment variables- 1. emacs 2. bash_profile 3. zshrc file

Steps to Set up the environment variable :-

  1. Download the jdk from JAVA
  2. install it by double click
  3. Now set-up environment variables in your file

    a. For emacs.profile you can use this link OR see the screenshot below enter image description here

    b. For ZSH profile setup -

    1. export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home
    
    2. source ~/.zshrc - Restart zshrc OR open another terminal 
    
    3. echo $JAVA_HOME - make sure path is set up properly 
       ----> /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home
    
    4. java -version 
    
       -->  java version "1.8.0_112"  Java(TM) SE Runtime Environment (build 1.8.0_112-b16)Java HotSpot(TM) 64-Bit Server VM (build 25.112-b16, mixed mode)
    

All set Now you can easily upgrade or degrade the JAVA version..