After upgrading to Lion from Snow Leopard, I ran into an immediate problem running unit tests for an application that uses JasperReports. JasperReports makes use of AWT and Swing regardless of whether the application calling it has a GUI interface. Normally this isn’t a problem, and it wasn’t a problem in Snow Leopard, but it has become one on Lion.

The error I received was:

java.lang.NoClassDefFoundError: apple/laf/CoreUIControl

The issue is that when the unit tests run, JasperReports tries to instantiate a Swing control and the Java run-time tries to initialize the default Apple Look-and-Feel. I’m not sure whether Lion has secured access to that (preventing unsigned applications from using it) or whether there’s another configuration issue in Lion. Regardless, the fix is simple for my purposes.

The Java default look-and-feel can be overridden at the command-line using a Java property:

-Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel

That will switch the Java instance from using the Apple Look-and-Feel to using Metal. BTW, running “headless” won’t fix the problem. Headless means telling the Java runtime not to make use of the GUI.

If you’re using Maven you might find that your unit tests still aren’t working, in that case, you need to tell Maven to pass the properties down to sub-processes:

mvn -DargLine="-Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel"

And, in case you’re wondering why you would want to run headless, it keeps macOS from treating your command-line app as a GUI app if it uses AWT or Swing. The property to trigger headless operation is:

-Djava.awt.headless=true