How to Revert to Java 1.6 on Mac OS X 10.7.5
Asked Answered
B

3

25

I have the 1.6 installer. I've used it. It does not change my Java installation, nor say there is an older version, but it does complete the installation.

I've been working with the symlinks a bit, but no matter what I do, running

java -version

in terminal always results in

Daves-MacBook-Pro:core-server dave$ java -version
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)

My application works with GAE, which does NOT use Java 1.7 at all. As such, I cannot compile my code using 1.7! I have to use 1.6, but I have failed at finding a way to remove 1.7 or otherwise force build/compiling to occur on 1.6.

A final note, I am running a build tool on the command line, so changing the settings of the project in Eclipse does not seem like it will help.

Bevash answered 27/11, 2012 at 23:8 Comment(1)
Do you not have the Oracle Java Preferences thing in your System Preferences? You can also do it manually a la #7731768, or you may be able to restore the old Java Preferences app if you have it backed up.Britisher
M
69

The java, javac, etc. command line tools are sensitive to the value of the JAVA_HOME environment variable and will use 1.6 if this variable points to a 1.6 JDK. The tool /usr/libexec/java_home is your friend here. Running

/usr/libexec/java_home

will print out the appropriate JAVA_HOME value for the most up to date JDK on your system. This will be Java 7, but you can apply constraints using the -v flag, for example

/usr/libexec/java_home -v '1.6*'

will return a JAVA_HOME value for the best available 1.6 JDK on your system. You can use this value to set JAVA_HOME:

export JAVA_HOME=`/usr/libexec/java_home -v '1.6*'`

either as a one-off for a particular Terminal session, or permanently for all future terminal sessions by adding the above line to the .bash_profile file in your home directory.


$ export JAVA_HOME=`/usr/libexec/java_home -v '1.6*'`
$ java -version
java version "1.6.0_37"
Java(TM) SE Runtime Environment (build 1.6.0_37-b06-434-11M3909)
Java HotSpot(TM) 64-Bit Server VM (build 20.12-b01-434, mixed mode)
$ export JAVA_HOME=`/usr/libexec/java_home -v '1.7*'`

$ java -version
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
Malayopolynesian answered 27/11, 2012 at 23:27 Comment(2)
I am using the export command to change the environment variable, JAVA_HOME - but when I then rerun /usr/libexec/java_home, it still returns 1.7.0 as the current JDK link! Is there something I need to do in order to force an update to JAVA_HOME other than run the command?Bevash
@Bevash I've edited my post to make things clearer - the /usr/libexec/java_home command doesn't tell you which Java you're currently using; its job is to find an appropriate value that you could put into JAVA_HOME.Malayopolynesian
H
1

If you need to write code that you want to run on a previous version of Java then you can change the compile flags. This might be all you need and

eg.

javac -source 1.6 -target 1.6 MyClass.java

The source arg states that the source is written in that version of Java, thus List<String> strings = new ArrayList<>(); would be a compile error. Target tells the compiler to compile byte code that is aimed at the specified version of the JVM. Though I think 1.7 is fully backwards compatible with 1.5 and 1.6.

Handmaid answered 28/11, 2012 at 14:55 Comment(2)
Almost, the only problem with this is that you're still compiling against the 1.7 class libraries. The compiler won't complain if you try and refer to a class or method that didn't exist in 1.6, but it will fail at runtime with a NoSuchMethodError. You need to install the 1.6 class library and set the compiler's bootstrap classpath to point to that to get it to catch these cases.Malayopolynesian
Yeah, I should have mentioned that. I've been caught out by that before with Swing. My suggestion was more of aHandmaid
Y
0

I ran into a similar problem. After having installed JDK7, some of my applications no longer worked. I needed to revert back to JDK6, and I did it differently. I noticed that in my /System/Library/Frameworks/JavaVM.framework/Versions/, it showed the following:

lrwxr-xr-x  1 root  wheel   10 Oct 25 17:01 1.4 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Oct 25 17:01 1.4.2 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Oct 25 17:01 1.5 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Oct 25 17:01 1.5.0 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Oct 25 17:01 1.6 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Oct 25 17:01 1.6.0 -> CurrentJDK
drwxr-xr-x  8 root  wheel  272 Oct 25 18:06 A
lrwxr-xr-x  1 root  wheel    1 Oct 25 17:01 Current -> A
lrwxr-xr-x  1 root  wheel   59 Nov 20 21:40 CurrentJDK -> /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/

so I removed the symbolic link CurrentJDK

 sudo rm CurrentJDK

and re-created the symbolic link pointing to JDK6, which is still on my Mac

sudo ln -s  /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/ CurrentJDK

and that did the trick for me

java -version
   java version "1.6.0_65"
   Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
   Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)
Yulandayule answered 21/11, 2013 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.