What a mess!
A program compiled with Java 6 does not work on my Mac after upgrading to Java 7. The message I get is
Exception in thread “main” java.lang.NoClassDefFoundError: com/apple/eawt/CocoaComponent
The problem is that com/apple/eawt/CocoaComponent class is a standard Apple JDK class that can be found in any JDK version up to 1.7 version, but in JDK 1.7 this class is gone… So if I do not want (or cannot) recompile the source, what can I do?
Well, I did upgrade my Mac to Java7, but previous versions of Java are sitting somewhere… And here is how I can run them:
/usr/libexec/java_home -v 1.6 --exec java
So, for instance this
/usr/libexec/java_home -v 1.6 --exec java -version
java version "1.6.0_51" Java(TM) SE Runtime Environment (build 1.6.0_51-b11-457-11M4509) Java HotSpot(TM) 64-Bit Server VM (build 20.51-b01-457, mixed mode)
Actually, the command allows running any specific Java version (you can specify also the subversion), provided it is available. If it not, the latest version will be launched. For instance
/usr/libexec/java_home -v 1.2.0 --exec java -version
on my mac gives the following result:
Unable to find any JVMs matching version "1.2.0". java version "1.7.0_17" Java(TM) SE Runtime Environment (build 1.7.0_17-b02) Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)