我遇到了一个 ‘NoClassDefFoundError’ 错误。

huangapple go评论77阅读模式
英文:

I am getting a 'NoClassDefFoundError'

问题

我刚开始学习LWJGL3,但出现了一些原因导致导入不起作用。我尝试了两种安装LWJGL3的方法,第一次我按照视频教程操作,第二次我按照官方安装指南进行安装([指南][1])。所有随LWJGL3一起提供的JAR文件都已包含在JRE系统库中。一旦我尝试使用glfw,就会出现错误。

[我的工作空间图片][2]

代码:

import static org.lwjgl.glfw.GLFW.*;
import org.lwjgl.glfw.GLFWVidMode;

public class Main {
public static void main(String[] args) {
if (!glfwInit()) {
throw new IllegalStateException("无法初始化GLFW");
}

    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    long window = glfwCreateWindow(640, 480, "LWJGL程序", glfwGetPrimaryMonitor(), 0);
    if(window == 0) {
        throw new IllegalStateException("无法创建窗口");
    }
    
    GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, (videoMode.width() - 640) / 2, (videoMode.height() - 480)/ 2);
    
    glfwShowWindow(window);
    
    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();
    }
}

}

错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/glfw/GLFW
    at Main.main(Main.java:6)
Caused by: java.lang.ClassNotFoundException: org.lwjgl.glfw.GLFW
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 1 more

编辑:类加载器无法加载该类,但我不知道为什么无法加载该类,因为它已添加到“外部JAR文件”中。[图片][3]


  [1]: https://github.com/LWJGL/lwjgl3-wiki/wiki/1.2.-Install
  [2]: https://i.stack.imgur.com/vRQxU.png
  [3]: https://i.stack.imgur.com/HHjBn.png
英文:

I am just starting to learn LWJGL3, but fot some reason, the imports don't work. I tried 2 methods of installing LWJGL3, first time I followed a video tutorial, second time I installed it how the official installation guide told me to (Guide). All of the JARs that come with LWJGL3 are included in the JRE System Library. The error appears as soon as I try to utilise glfw.

Image of my workspace

Code:

import static org.lwjgl.glfw.GLFW.*;
import org.lwjgl.glfw.GLFWVidMode;

public class Main {
	public static void main(String[] args) {
		if (!glfwInit()) {
			throw new IllegalStateException("Failed to initialise GLFW");
		}
		
		glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
		long window = glfwCreateWindow(640, 480, "LWJGL Porgramme", glfwGetPrimaryMonitor(), 0);
		if(window == 0) {
			throw new IllegalStateException("Failed to create window");
		}
		
		GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
		glfwSetWindowPos(window, (videoMode.width() - 640) / 2, (videoMode.height() - 480)/ 2);
		
		glfwShowWindow(window);
		
		while (!glfwWindowShouldClose(window)) {
			glfwPollEvents();
		}
	}
}

Error:


    Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/glfw/GLFW
    	at Main.main(Main.java:6)
    Caused by: java.lang.ClassNotFoundException: org.lwjgl.glfw.GLFW
    	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    	... 1 more

Edit: ClassLoader can't load the class, but I don't know why it can't load the class, since it is added in "external JARs". Image

答案1

得分: 0

这花了太长时间,但我让它运行起来了。我使用Gradle安装了LWJGL3,创建了一个Gradle项目,并将Java源代码复制到了Gradle项目中。

英文:

This took way too long, but I got it to work. I installed LWJGL3 with Gradle, made a Gradle project and copied the Java source code to the Gradle project.

huangapple
  • 本文由 发表于 2020年10月12日 02:40:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64307748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定