Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Revert to Java 1.6 on Mac OS X 10.7.5

Tags:

java

macos

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.

like image 907
Davek804 Avatar asked Sep 10 '25 08:09

Davek804


2 Answers

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)
like image 152
Ian Roberts Avatar answered Sep 13 '25 00:09

Ian Roberts


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.

like image 25
Dunes Avatar answered Sep 13 '25 00:09

Dunes