Monday, December 13, 2010

On a Mac, Eclipse's Java compiler does not use your JDK compiler

I spent a lot of time recently finding out why Eclipse 3.5 (Galileo) on my Mac wouldn't compile Java files using the JDK 1.5 that I installed. Apparently, Eclipse uses the JNI "JavaVM" library to run itself and compile files. It doesn't use the installed JDK's!

From http://wiki.eclipse.org/Equinox_Launcher#Exceptions:

"MacOSX: On the mac we are always launching via the JNI invocation API. We use "/System/Library/Frameworks/JavaVM.framework/Versions/Current/JavaVM" as the jvm library. When a -vm argument is given, we parse it for the version of java to specify to JavaVM."

What this means is that even if you specify a particular VM to use in your eclipse.ini file by using the "-vm" argument, Eclipse still won't use that VM for launching itself or for compiling files! Passing "1.5" to a 1.6 JavaVM won't make it a 1.5 VM (duh)!

So if your Java compiler compliance level is 1.5 and you see, "When selecting 1.5 compliance, make sure to have a compatible JRE installed and activated (currently 1.6). Configure..." in your Java Compiler Preferences, most likely your JavaVM file is 1.6 and not 1.5!


Here's what my Java Versions subdirectory looks like, /System/Library/Frameworks/JavaVM.framework/Versions :

$ ls -al
total 300816
drwxr-xr-x 16 root wheel 544 Dec 2 15:10 .
drwxr-xr-x 12 root wheel 408 Aug 3 15:01 ..
lrwxr-xr-x 1 root wheel 5 Aug 3 14:59 1.3 -> 1.3.1
drwxr-xr-x 3 root wheel 102 Feb 11 2010 1.3.1
lrwxr-xr-x 1 root wheel 10 Aug 3 14:59 1.4 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 Aug 3 14:59 1.4.2 -> CurrentJDK
lrwxr-xr-x 1 root wheel 5 Dec 2 11:11 1.5 -> 1.5.0
drwxr-xr-x 8 root wheel 272 Dec 2 11:52 1.5.0
lrwxr-xr-x 1 root wheel 5 Aug 3 14:59 1.6 -> 1.6.0
drwxr-xr-x 8 root wheel 272 Aug 9 12:32 1.6.0
drwxr-xr-x 8 root wheel 272 Feb 9 2010 A
lrwxr-xr-x 1 root wheel 1 Aug 3 14:59 Current -> A
lrwxr-xr-x 1 root wheel 5 Dec 2 15:10 CurrentJDK -> 1.5.0

See that Current is a symlink to the "A" subdirectory? That's where JavaVM lives:

$ cd A
$ ls -al
total 408
drwxr-xr-x 8 root wheel 272 Feb 9 2010 .
drwxr-xr-x 16 root wheel 544 Dec 2 15:10 ..
lrwxr-xr-x 1 root wheel 28 Feb 10 2010 CodeResources -> _CodeSignature/CodeResources
drwxr-xr-x 42 root wheel 1428 Feb 9 2010 Commands
drwxr-xr-x 4 root wheel 136 Jul 20 2009 Frameworks
-rwxr-xr-x 1 root wheel 201232 Nov 12 2009 JavaVM
drwxr-xr-x 32 root wheel 1088 Feb 9 2010 Resources
drwxr-xr-x 3 root wheel 102 Feb 9 2010 _CodeSignature

I had to replace the "A" subdirectory with files from someone else's Mac.

1 comment:

AGajdy said...

Can you provide this subdirectory?