java.lang.NoClassDefFoundError: 無法初始化 class java.awt.Toolkit

2022-09-03 16:28:27

我正在开发XUbuntu 13.04并开发swing应用程序。由于我只是想尝试启动时遇到的应用程序。java.lang.NoClassDefFoundError: Could not initialize class java.awt.Toolkit

我发现了几个关于服务器/客户端VM的问题,并注意到我有一个服务器VM,而对于Linux 64,不再有客户端VM。但恕我直言,这个问题不应该取决于我运行的虚拟机。

有没有人注意到这个问题并解决了它?我做错了什么?使用64位jdk,一切正常,但是当我尝试使用32位jdk时,启动失败。

提前致谢!

附言:Java版本与1.7.0_251.7.0_09

编辑

使用无外设模式时,会发生以下异常:

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)

答案 1

好了,我终于找到了答案。感谢您的努力!问题是我已经安装了它,但只有它的64位版本。libxtst6

sudo apt-get install libxtst6:i386为我做了伎俩。

我是怎么找到这个答案的?我稍微整理了一下,但什么都没有,正如你在下面看到的:ldd

$ 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

这就是我结束信任我的IDE并在命令行上启动示例应用程序的时候。命令行再次对我很好:

$ 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)

答案 2

在 Ubuntu 20.04 上缺少 awt 库问题。通过安装openjdk-11-jre修复了这个问题:

sudo apt-get install openjdk-11-jre

推荐