20

I'm working on XUbuntu 13.04 and developing swing applications. As I just wanted to try that application I ran into java.lang.NoClassDefFoundError: Could not initialize class java.awt.Toolkit on startup.

I've found several questions about server/client vms and noticed that I have a server VM and that for Linux 64 there is no client VM anymore. But nevertheless IMHO this problem shouldn't depend on which VM I'm running on.

Has anybody else noticed that problem and has solved it? What am I doing wrong? With 64bit jdk everything works fine, but when I try to use the 32bit jdk the startup fails.

Thanks in advance!

P.S.: Java version is 1.7.0_25 and same with 1.7.0_09

EDIT

When using headless mode, the following exceptions occur:

java.lang.reflect.InvocationTargetException
    at java.awt.EventQueue.invokeAndWait(EventQueue.java:1272)
    at java.awt.EventQueue.invokeAndWait(EventQueue.java:1247)
    at javax.swing.SwingUtilities.invokeAndWait(SwingUtilities.java:1349)
    at Main.main(Main.java:218)
Caused by: java.awt.HeadlessException
    at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:207)
    at java.awt.Window.<init>(Window.java:535)
    at java.awt.Frame.<init>(Frame.java:420)
    at java.awt.Frame.<init>(Frame.java:385)
    at javax.swing.JFrame.<init>(JFrame.java:180)
    at ..MyFrame.<init>(MyFrame.java:23)
Bertram Nudelbach
  • 1,783
  • 2
  • 15
  • 27
  • 1
    Can be the problem described [here](http://stackoverflow.com/questions/5576986/servlet-giving-error-java-lang-noclassdeffounderror) – Luca Basso Ricci Aug 07 '13 at 09:32
  • 1
    I considered that option, but with `-Djava.awt.headless=true` there are a couple of `HeadlessException`s. Is this option meant to work with swing applications, too? – Bertram Nudelbach Aug 07 '13 at 09:35
  • Are exception similar to that described [here](http://stackoverflow.com/questions/5218870/getting-a-headlessexception-no-x11-display-variable-was-set)? – Luca Basso Ricci Aug 07 '13 at 09:42
  • Similar, but no message with X11. – Bertram Nudelbach Aug 07 '13 at 09:49
  • http://stackoverflow.com/questions/5362512/unable-to-run-java-gui-programs-with-ubuntu https://www.ibm.com/developerworks/community/blogs/738b7897-cd38-4f24-9f05-48dd69116837/entry/how_to_resolve_java_exceptions_java_awt_headlessexception7?lang=en – Luca Basso Ricci Aug 07 '13 at 09:52
  • @bellabax thanks for the many links! I've installed `libxtst6`, `default-jdk`, `default-jre`, `openjdk-6-jdk`, `openjdk-6-jre`, `openjdk-7-jdk`, `openjdk-7-jre`. All of these are mentioned by different Q&A sessions online .. problem still occurs. I've no server/client-application, it's just a client containing the UI. `$ echo $DISPLAY` results in `:0.0`. I've no idea what I could try else.. maybe I should search if it's even possible to run applications with 32bit java on 64bit machines. – Bertram Nudelbach Aug 07 '13 at 10:24
  • 1
    More alternatives [here](http://stackoverflow.com/a/13985919/230513). – trashgod Aug 07 '13 at 10:38
  • `Exception in thread "main" java.awt.AWTError: Could not instantiate Toolkit: sun.awt.HeadlessToolkit` – Bertram Nudelbach Aug 07 '13 at 11:24

3 Answers3

45

Okay, finally I've found the answer. Thanks for the effort! The problem is that I had libxtst6 installed but only the 64bit-version of it.

sudo apt-get install libxtst6:i386 did the trick for me.

How did I find this answer? I ldded around a bit, but nothing, as you can see in the following:

$ ldd jdk1.7.0_25_x86/jre/lib/i386/xawt/libmawt.so
    linux-gate.so.1 =>  (0xf7769000)
    libpthread.so.0 => /lib/i386-linux-gnu/libpthread.so.0 (0xf76ee000)
    libm.so.6 => /lib/i386-linux-gnu/libm.so.6 (0xf76c2000)
    libawt.so => jdk1.7.0_25_x86/jre/lib/i386/xawt/../libawt.so (0xf760f000)
    libXext.so.6 => /usr/lib/i386-linux-gnu/libXext.so.6 (0xf75fd000)
    libX11.so.6 => /usr/lib/i386-linux-gnu/libX11.so.6 (0xf74c9000)
    libXrender.so.1 => /usr/lib/i386-linux-gnu/libXrender.so.1 (0xf74bf000)
    libdl.so.2 => /lib/i386-linux-gnu/libdl.so.2 (0xf74ba000)
    libXtst.so.6 => /usr/lib/i386-linux-gnu/libXtst.so.6 (0xf74b2000)
    libXi.so.6 => /usr/lib/i386-linux-gnu/libXi.so.6 (0xf74a2000)
    libjava.so => jdk1.7.0_25_x86/jre/lib/i386/xawt../libjava.so (0xf747e000)
    libjvm.so => not found
    libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xf72d5000)
    /lib/ld-linux.so.2 (0xf776a000)
    libjvm.so => not found
    libxcb.so.1 => /usr/lib/i386-linux-gnu/libxcb.so.1 (0xf72b3000)
    libjvm.so => not found
    libverify.so => java/jdk1.7.0_25_x86/jre/lib/i386/xawt/../libverify.so (0xf72a7000)
    libXau.so.6 => /usr/lib/i386-linux-gnu/libXau.so.6 (0xf72a2000)
    libXdmcp.so.6 => /usr/lib/i386-linux-gnu/libXdmcp.so.6 (0xf729b000)
    libjvm.so => not found

So that was the point when I ended trusting my IDE and started a sample application on the command line. And the command line again was nice to me:

$ jdk1.7.0_25_x86/bin/java Main
Exception in thread "main" java.lang.UnsatisfiedLinkError: jdk1.7.0_25_x86/jre/lib/i386/xawt/libmawt.so: libXtst.so.6: cannot open shared object file: No such file or directory
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1957)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1882)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1843)
    at java.lang.Runtime.load0(Runtime.java:795)
    at java.lang.System.load(System.java:1061)
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1957)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1882)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864)
    at java.lang.Runtime.loadLibrary0(Runtime.java:849)
    at java.lang.System.loadLibrary(System.java:1087)
    at sun.security.action.LoadLibraryAction.run(LoadLibraryAction.java:67)
    at sun.security.action.LoadLibraryAction.run(LoadLibraryAction.java:47)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.awt.Toolkit.loadLibraries(Toolkit.java:1646)
    at java.awt.Toolkit.<clinit>(Toolkit.java:1668)
    at java.awt.Component.<clinit>(Component.java:595)
    at Main.main(Main.java:5)
slm
  • 15,396
  • 12
  • 109
  • 124
Bertram Nudelbach
  • 1,783
  • 2
  • 15
  • 27
  • 1
    this fixed issue : `Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class java.awt.Toolkit` – jsdev Oct 29 '14 at 08:46
  • 3
    I was missing another library, but your comment helped to guide me, thanks ! For me it was libxi6:i386 that was missing. – Gonzague Aug 02 '18 at 08:17
23

Had the missing awt library problem on Ubuntu 20.04. Fixed it by installing openjdk-11-jre:

sudo apt-get install openjdk-11-jre
wedesoft
  • 2,781
  • 28
  • 25
  • 2
    Thanks, this worked for me :) Obviously as to `openjdk-XX-jre`, one should specify the correct version for `XX`. – Celdor Aug 25 '21 at 15:44
  • Thank you! This was the solution for me to get Studio3T install to work. Just a note: after you install openjdk as above, you need to **reload your terminal** before re-trying the command that was giving you the error. – user3616725 Aug 15 '23 at 13:42
5

Thanks for @Bertram's answer. As a complementary, I met similar issue when I'm using Fedora, for anyone who is using CentOS/Fedora and Redhat, please try yum install libXtst.

Eugene
  • 10,627
  • 5
  • 49
  • 67
  • 1
    FYI, I had to install just the `java-17-openjdk` (I had probably only some older java versions after my Fedora upgrades). – Jakuje Oct 25 '22 at 16:09