javaswingawtlwjgl

LWJGL AWTGLCanvas.render() causes java.lang.NoSuchMethodError


I installed LWJGL to study how opengl works and used the code from this publicly posted site. https://github.com/LWJGLX/lwjgl3-awt?tab=readme-ov-file

package com.myCompany;

import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.awt.AWTGLCanvas;
import org.lwjgl.opengl.awt.GLData;

import javax.swing.*;

import java.awt.*;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL45.*;
import static org.lwjgl.opengl.GL.createCapabilities;

public class GLTest {


    public static void main(String[] args) {
        GLData data = new GLData();
        AWTGLCanvas canvas = new AWTGLCanvas(data) {

            @Override
            public void initGL() {
                glfwInit();
                createCapabilities();
                glClearColor(0.3f, 0.4f, 0.5f, 1);
            }

            @Override
            public void paintGL() {
                glClear(GL_COLOR_BUFFER_BIT);
                glEnd();
                swapBuffers();
            }
        };
        JFrame wnd = new JFrame();
        wnd.setLayout(new BorderLayout());
        wnd.setPreferredSize(new Dimension(500, 500));
        wnd.setVisible(true);
        wnd.pack();
        wnd.transferFocus();
        wnd.add(canvas);

        Runnable renderLoop = new Runnable() {
            @Override
            public void run() {
                if (!canvas.isValid()) {
                    GL.setCapabilities(null);
                    return;
                }
                canvas.render();
                SwingUtilities.invokeLater(this);
            }
        };
        SwingUtilities.invokeLater(renderLoop);
    }
}

But it throws the following exception at canvas.render():

Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: 'short org.lwjgl.system.windows.User32.RegisterClassEx(org.lwjgl.system.windows.WNDCLASSEX)'
    at org.lwjgl.opengl.awt.PlatformWin32GLCanvas.createDummyWindow(PlatformWin32GLCanvas.java:134)
    at org.lwjgl.opengl.awt.PlatformWin32GLCanvas.create(PlatformWin32GLCanvas.java:154)
    at org.lwjgl.opengl.awt.AWTGLCanvas.beforeRender(AWTGLCanvas.java:60)
    at org.lwjgl.opengl.awt.AWTGLCanvas.render(AWTGLCanvas.java:101)
    at com.myCompany.GLTest$2.run(GLTest.java:52)

Here is a part of the pom.xml file.

<properties>
        <maven.compiler.source>23</maven.compiler.source>
        <maven.compiler.target>23</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <lwjgl.version>3.3.6</lwjgl.version>
        <joml.version>1.10.7</joml.version>
        <lwjgl3-awt.version>0.1.8</lwjgl3-awt.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.lwjgl</groupId>
                <artifactId>lwjgl-bom</artifactId>
                <version>${lwjgl.version}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <profiles>
        <profile>
            <id>lwjgl-natives-linux-amd64</id>
            <activation>
                <os>
                    <family>unix</family>
                    <name>linux</name>
                    <arch>amd64</arch>
                </os>
            </activation>
            <properties>
                <lwjgl.natives>natives-linux</lwjgl.natives>
            </properties>
        </profile>
        <profile>
            <id>lwjgl-natives-macos-x86_64</id>
            <activation>
                <os>
                    <family>mac</family>
                    <arch>x86_64</arch>
                </os>
            </activation>
            <properties>
                <lwjgl.natives>natives-macos</lwjgl.natives>
            </properties>
        </profile>
        <profile>
            <id>lwjgl-natives-windows-amd64</id>
            <activation>
                <os>
                    <family>windows</family>
                    <arch>amd64</arch>
                </os>
            </activation>
            <properties>
                <lwjgl.natives>natives-windows</lwjgl.natives>
            </properties>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>org.lwjgl</groupId>
            <artifactId>lwjgl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.lwjgl</groupId>
            <artifactId>lwjgl-glfw</artifactId>
        </dependency>
        <dependency>
            <groupId>org.lwjgl</groupId>
            <artifactId>lwjgl-opengl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.lwjgl</groupId>
            <artifactId>lwjgl</artifactId>
            <classifier>${lwjgl.natives}</classifier>
        </dependency>
        <dependency>
            <groupId>org.lwjgl</groupId>
            <artifactId>lwjgl-glfw</artifactId>
            <classifier>${lwjgl.natives}</classifier>
        </dependency>
        <dependency>
            <groupId>org.lwjgl</groupId>
            <artifactId>lwjgl-opengl</artifactId>
            <classifier>${lwjgl.natives}</classifier>
        </dependency>
        <dependency>
            <groupId>org.joml</groupId>
            <artifactId>joml</artifactId>
            <version>${joml.version}</version>
        </dependency>
        <dependency>
            <groupId>org.lwjglx</groupId>
            <artifactId>lwjgl3-awt</artifactId>
            <version>${lwjgl3-awt.version}</version>
        </dependency>
</dependencies>

It worked fine in a Linux environment with the same conditions.

The OS that is causing the problem is windows 11, and The native library is windows-amd64.

The native library for Linux OS is linux-amd64.

Am I missing something?


Solution

  • This issue was caused by a version mismatch between the awt library 0.1.8 and the lwjgl library 3.3.6. This issue is resolved by updating the awt library to 0.2.3 or downgrading the lwjgl library to 3.3.3.

    When updating to 3.3.6, I found that the method was fixed, which is why this error occurs.

    I was unable to download awt 0.2.3, but rebooting my PC fixed it.