NoClassDefFoundError/ClassNotFoundException while using SWT


I'm stuck with this issue for like 10h, now.

mvn clean compile install compiles the code just fine. The problem appears when I try to run the bundled JAR, built to include all dependencies. FYI, it happens while trying to run the project inside Eclipse as well.

I tried using -verbose:class as VM args while running it by the java jar and through the Eclipse IDE. The output shows the JVM loading the "buggy" class, but I still get the exception.

Tried a lot of other options as well, like extracting the SWT native libraries to the same path as the JAR, but that didn't work either. Cleaning the relevant part of my m2 cache didn't help. I also double-checked the bundled JAR for the missing class and, guess what? It's right there.

As most of you may already have understood, this is part of a much bigger project, for which a small test case could not be (easily) created. I tried to create a small sample (without maven) and it's working. Another (probably) relevant piece of info is that I'm using both, SWT and JavaFX in this same class, following working recipes on how to mix them. And I'm using Oracle JDK 1.7.

UPDATE: tried to move the "small sample" to Maven and it's running ok, so far. I suppose the problem is not related to the maven dependency or something.

UPDATE: I managed to create a small project that have the same problem. It's an Eclipse project, but should be easily run without IDE (since it's a Maven project). It depends on the Linux x64 version of SWT, but you can change that dependency as well. Please, check it out at

The exception stack trace:

Exception in thread "main" 
java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Canvas
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(
    at Method)
    at java.lang.ClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(
    at java.lang.Class.getMethod0(
    at java.lang.Class.getMethod(
    at sun.launcher.LauncherHelper.getMainMethod(
    at sun.launcher.LauncherHelper.checkAndLoadMain(
[Loaded java.util.Objects from /usr/lib/jvm/java-7-oracle/jre/lib/rt.jar]
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Canvas
    at Method)
    at java.lang.ClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    ... 19 more

The -verbose:class output:

[Loaded org.eclipse.swt.widgets.Layout from file:/home/me/myapp-jar-with-dependencies.jar]
[Loaded org.eclipse.swt.layout.FillLayout from file:/home/me/myapp-jar-with-dependencies.jar]
[Loaded from file:/home/me/myapp-jar-with-dependencies.jar]
[Loaded org.eclipse.swt.widgets.Widget from file:/home/me/myapp-jar-with-dependencies.jar]
[Loaded org.eclipse.swt.widgets.Control from file:/home/me/myapp-jar-with-dependencies.jar]
[Loaded org.eclipse.swt.widgets.Scrollable from file:/home/me/myapp-jar-with-dependencies.jar]
[Loaded org.eclipse.swt.widgets.Composite from file:/home/me/myapp-jar-with-dependencies.jar]
[Loaded org.eclipse.swt.widgets.Canvas from file:/home/me/myapp-jar-with-dependencies.jar]
[Loaded org.eclipse.swt.widgets.Decorations from file:/home/me/myapp-jar-with-dependencies.jar]
[Loaded org.eclipse.swt.widgets.Shell from file:/home/me/myapp-jar-with-dependencies.jar]
[Loaded java.lang.ClassFormatError from /usr/lib/jvm/java-7-oracle/jre/lib/rt.jar]
[Loaded java.lang.NoSuchMethodException from /usr/lib/jvm/java-7-oracle/jre/lib/rt.jar]
[Loaded java.lang.Throwable$PrintStreamOrWriter from /usr/lib/jvm/java-7-oracle/jre/lib/rt.jar]
[Loaded java.lang.Throwable$WrappedPrintStream from /usr/lib/jvm/java-7-oracle/jre/lib/rt.jar]
[Loaded java.util.IdentityHashMap from /usr/lib/jvm/java-7-oracle/jre/lib/rt.jar]
[Loaded java.util.IdentityHashMap$KeySet from /usr/lib/jvm/java-7-oracle/jre/lib/rt.jar]
[Loaded java.util.Objects from /usr/lib/jvm/java-7-oracle/jre/lib/rt.jar]
[Loaded java.lang.Shutdown from /usr/lib/jvm/java-7-oracle/jre/lib/rt.jar]
[Loaded java.lang.Shutdown$Lock from /usr/lib/jvm/java-7-oracle/jre/lib/rt.jar]

The relevant part of my POM:


Any clue? If more information is needed, be my guest and ask for it, please.

Thanks for any help.

11/19/2013 10:46:10 PM

Accepted Answer

After all, it was a class loading dependency problem. JavaFX FXCanvas was being loaded before the SWT classes.

The solution was to add SWT platform specific JAR to the bootclasspath, using the -Xbootclasspath/a when running the bundled JAR.

This appends the SWT JAR to the bootclasspath, making it available for JavaFX classes, when they need it.

Note: it's also possible to do this tweaking most IDEs run/debug configs, if you need it.

11/27/2013 8:41:21 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow