Marcin's Musings

Random bits of stuff

Mac OS X Java and /Library/Java/Extensions Based Woes

| Comments

The kid was asleep, the wife had gone to bed early, so I did what any other sane person would do on a Saturday night – cracked open a beer and decided to brush up on my Java skills.

I’ve never done Java full-time, and while I know the basics of the language – I even got a “Sun Certified Java Programmer 1.4” certification back in the day, just for kicks – I hadn’t explored the newer features like Generics, Annotations and whatever else. So with that in mind I fired up IDEA and started playing around.

I soon ran into some troubles though!

When I switched my project to point at the 1.7 JDK, IDEA was telling me:

java: System Java Compiler was not found in classpath

A Stackoverflow post suggested using the “Eclipse” compiler instead of “Javac”, and this seemed to do the trick, but I didn’t know why so that wasn’t ideal. I checked my JAVA_HOME variable and all was good:

/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home

Running javac -version from the command line gave me the right version too:

javac 1.7.0_51

So no idea why Javac mode wasn’t working, but I went back to playing around.

Soon after I wanted to check out Junit, so followed the instructions to add that to my project using Maven (now I have two problems!). I’m new to Maven as well, so I was relatively blindly just adding the JUnit dependency and using IDEA to sort the rest out for me and hoping for the best.

The problem now I was that I couldn’t compile using maven. It was giving me an unsual error:

class file has wrong version 51.0, should be 49.0

… and this error was in reference to java.util.List!

I tried compiling from the command line using javac and got an error illegal start of type where I was using the Java 1.7 “diamond” notation:

1
List<String> strings = new ArrayList<>();

javac -version and my JAVA_HOME were definitely pointing to the 1.7 JDK so I was very confused. After some additional research, I tried doing:

javac -source 1.7 -target 1.7 Foo.java

but kept getting errors like: invalid source release: 1.7 – which were very perplexing! -target 1.6 didn’t work either, but -target 1.5 did, which was very strange.

A bunch of Google searches for these various errors I’d been having yielded no useful answers for my situation – basically most of them boiled down to invalid JAVA_HOME settings, but mine were fine!

So to cut a long story short, and it’s already been spoiled if you’ve read the title, there’s a /Library/Java/Extensions directory on Mac OS X that contains additional JAR files that are on the default classpath. These JARs can be installed by other applications, and can cause conflicts with other JARs you’re expecting. I removed all the JARs from this folder and suddenly everything worked fine! IDEA could compile in Javac mode! Maven compiled! javac from the command line compiled!

I suspect the culprit was the tools.jar that had been placed there by something. This tools.jar was, I assume, from the Java 1.5 JDK. tools.jar appears to contain the classes for the Java Compiler, amongst other things. So despite the command line compiler being from 1.7, the actual classes that it was loading to do the compilation were from Java 1.5!

It took me two hours or so of futile searching, experimentation, reinstalling of JDKs, etc, before I finally remembered that I’d been bitten by /Library/Java/Extensions before. Should’ve thought of it sooner!

Anyway, this post probably won’t be useful unless you’re in this particular situation, but my hope is that by adding all those error messages in the post and the solution, somebody else who runs into the same issue will find my post instead of wasting time trying to work it out!

Comments