FreshRSS

Normální zobrazení

Jsou dostupné nové články, klikněte pro obnovení stránky.
PředevčíremHlavní kanál
  • ✇Recent Questions - Game Development Stack Exchange
  • How do I fix java.lang.ClassNotFoundException: org.lwjgl.glfw.GLFWDoggo4
    I have not used Java in a while and thought I might try LWJGL with OpenGL and GLFW. I am using Apache Maven as a Build System. It lets me compile the program, but when I run it, it says: Exception in thread "main" java.lang.NoClassDefFoundError: org.lwjgl/glfw/GLFW at com.OpenGLTest.app.Main.main(Main.java:25) Caused by: java.lang.ClassNotFoundException: org.lwjgl.glfw.GLFW at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java.641) at java.base/jdk.int
     

How do I fix java.lang.ClassNotFoundException: org.lwjgl.glfw.GLFW

I have not used Java in a while and thought I might try LWJGL with OpenGL and GLFW. I am using Apache Maven as a Build System. It lets me compile the program, but when I run it, it says:

Exception in thread "main" java.lang.NoClassDefFoundError: org.lwjgl/glfw/GLFW
    at com.OpenGLTest.app.Main.main(Main.java:25)
Caused by: java.lang.ClassNotFoundException: org.lwjgl.glfw.GLFW
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java.641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
    ... 1 more

My code is:

// Main.java
package com.OpenGLTest.app;

import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;

import java.nio.*;

import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;

public class Main {

  private static long window;
  private static final int WIDTH = 800;
  private static final int HEIGHT = 600;
  private static final String TITLE = "OpenGL Window";

  public static void main(String[] args) {
    // CHECK
    if (!glfwInit()) {
      System.err.println("ERROR: GLFW IS NOT INSTALLED");
      System.exit(-1);
    }
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    glfwWindowHint(GLFW_VISIBLE, 0);
    glfwWindowHint(GLFW_RESIZABLE, 0);

    window = glfwCreateWindow(WIDTH, HEIGHT, TITLE, NULL, NULL);
    if (window == NULL) {
      System.err.println("ERROR: FAILED TO CREATE GLFW WINDOW");
    }

    glfwMakeContextCurrent(window);

    glfwShowWindow(window);
  }
}

My LWJGL version is 3.3.3 My JRE is 17

I am sorry if the answer is obvious. Somehow the only kinda answer I found on the internet is http://forum.lwjgl.org/index.php?topic=6994.0:

https://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java

How to solve this entirely depends on how you invoke the java command, whether you use an >IDE and which one you use, whether you use the Java 9+ Module System or the classpath and >whether you use a Java build system (like Maven, Gradle, Ant+Ivy).

  • ✇Recent Questions - Game Development Stack Exchange
  • When compiling shaders in OpenGL, I get random error messagesterraquad
    I am trying to follow LearnOpenGL while coding in the Zig language, and something that is very odd is that sometimes my shader compilation fails even though I changed nothing between executing the app. I also don't understand the errors. By the way, I use mach-glfw and zigglgen. Some errors I get: error: [opengl] Failed to compile shader: assets/shaders/shader.frag 0(8) : error C0000: syntax error, unexpected '=' at token "=" error: [opengl] Failed to link shader program: Fragment info ----
     

When compiling shaders in OpenGL, I get random error messages

I am trying to follow LearnOpenGL while coding in the Zig language, and something that is very odd is that sometimes my shader compilation fails even though I changed nothing between executing the app. I also don't understand the errors. By the way, I use mach-glfw and zigglgen.

Some errors I get:

error: [opengl] Failed to compile shader: assets/shaders/shader.frag
  0(8) : error C0000: syntax error, unexpected '=' at token "="

error: [opengl] Failed to link shader program:
  Fragment info
-------------
0(8) : error C0000: syntax error, unexpected '=' at token "="
(0) : error C2003: incompatible options for link
error: [opengl] Failed to compile shader: assets/shaders/shader.frag
  0(9) : error C0000: syntax error, unexpected '(', expecting ';' at token "("

error: [opengl] Failed to link shader program:
  Fragment info
-------------
0(9) : error C0000: syntax error, unexpected '(', expecting ';' at token "("
(0) : error C2003: incompatible options for link

error: [opengl] Failed to compile shaders/shader.vert:
0(6) : error C0000: syntax error, unexpected $undefined at token "<undefined>"

Here is the code:

// Vertex shader
#version 330 core
layout (location = 0) in vec3 aPos;

out vec4 vertexColor;

void main() {
    gl_Position = vec4(aPos.xyz, 1.0);
    vertexColor = vec4(0.5, 0.0, 0.0, 1.0);
}
// Fragment shader
#version 330 core
out vec4 FragColor;
  
in vec4 vertexColor;

void main() {
    FragColor = vertexColor;
}
// Shortened main code
const std = @import("std");
const builtin = @import("builtin");
const glfw = @import("glfw");
const gl = @import("gl");
const App = @import("App.zig");

var gl_procs: gl.ProcTable = undefined;

fn glfwErrorCallback(err: glfw.ErrorCode, desc: [:0]const u8) void {
    ...
}

fn glfwFramebufferSizeCallback(_: glfw.Window, w: u32, h: u32) void {
    gl.Viewport(0, 0, @intCast(w), @intCast(h));
}

pub fn main() !void {
    // GLFW initialization
    if (!glfw.init(.{})) {
        ...
    }
    defer glfw.terminate();

    // Window creation
    const window = glfw.Window.create(1280, 720, "example opengl app", null, null, .{
        ...
    }) orelse {
        ...
    };
    defer window.destroy();
    glfw.makeContextCurrent(window);

    // OpenGL preparation
    if (!gl_procs.init(glfw.getProcAddress)) {
        ...
    }
    gl.makeProcTableCurrent(&gl_procs);
    window.setFramebufferSizeCallback(glfwFramebufferSizeCallback);

    // App startup
    var app = App{
        .window = window,
    };
    app.run() catch |err| {
        ...
    };
}

// shortened App code
...

window: glfw.Window,
vertices: [12]f32 = [_]f32{
    0.5,  0.5,  0.0,
    0.5,  -0.5, 0.0,
    -0.5, -0.5, 0.0,
    -0.5, 0.5,  0.0,
},
indices: [6]gl.uint = [_]gl.uint{
    0, 1, 3,
    1, 2, 3,
},

fn createCompiledShader(file: []const u8, stype: Shader.ShaderType) !Shader {
    const shader = try Shader.fromFile(file, stype, std.heap.raw_c_allocator);
    if (shader.compile()) |msg| {
        std.log.err("[opengl] Failed to compile shader: {s}\n  {s}", .{ file, msg });
    }
    return shader;
}

pub fn run(this: App) !void {
    // == STARTUP

    // Create vertex array object
    ...

    // Create vertex buffer object
    ...

    // Create element buffer object
    ...

    // Vertex attributes
    ...

    // Create shaders
    const vertex_shader = try createCompiledShader("assets/shaders/shader.vert", .Vertex);
    const fragment_shader = try createCompiledShader("assets/shaders/shader.frag", .Fragment);

    // Create shader program
    const shader_program = ShaderProgram.init(&.{
        vertex_shader,
        fragment_shader,
    });
    if (shader_program.link()) |msg| {
        std.log.err("[opengl] Failed to link shader program:\n  {s}", .{msg});
    }
    // Activate program and delete shaders
    shader_program.use();
    vertex_shader.delete();
    fragment_shader.delete();

    // == RENDER LOOP

    while (!this.window.shouldClose()) {
        gl.ClearColor(0.5, 0.3, 0.1, 1.0);
        gl.Clear(gl.COLOR_BUFFER_BIT);

        shader_program.use();
        gl.BindVertexArray(vao);
        gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, 0);

        this.window.swapBuffers();
        glfw.pollEvents();
    }
}
// shortened Shader class
...

pub const ShaderType = enum {
    Vertex,
    Fragment,
};

/// The source code of the shader.
source: []const u8,
gl_object: gl.uint,

fn createOpenglShader(src: []const u8, shader_type: ShaderType) gl.uint {
    const stype: gl.uint = switch (shader_type) {
        .Vertex => gl.VERTEX_SHADER,
        .Fragment => gl.FRAGMENT_SHADER,
    };
    const object: gl.uint = gl.CreateShader(stype);
    gl.ShaderSource(object, 1, @ptrCast(&src.ptr), null);
    return object;
}

/// Creates a `Shader` object from the source string.
pub fn fromString(src: []const u8, shader_type: ShaderType) Shader {
    ...
}

/// Creates a `Shader` object from the file contents of the given `path`.
/// The file path has to be relative to the folder where the executable resides.
/// If you want to use a file outside of that folder, open the file yourself and pass its' contents to `Shader.fromString`.
/// For some reason, you can't pass a `GeneralPurposeAllocator` since the program segfaults if you do.
pub fn fromFile(path: []const u8, shader_type: ShaderType, alloc: std.mem.Allocator) !Shader {
    var arena = std.heap.ArenaAllocator.init(alloc);
    defer arena.deinit();
    const allocator = arena.allocator();

    var root_dir = try std.fs.openDirAbsolute(try std.fs.selfExeDirPathAlloc(allocator), .{});
    defer root_dir.close();

    var file = try root_dir.openFile(path, .{});
    defer file.close();

    const buf = try allocator.alloc(u8, try file.getEndPos());
    _ = try file.readAll(buf);

    return .{
        .source = buf,
        .gl_object = createOpenglShader(buf, shader_type),
    };
}

/// Compiles the shader. If compilation succeeded, the return value will be null.
/// Otherwise, the return value will be the error message.
pub fn compile(self: Shader) ?[256]u8 {
    gl.CompileShader(self.gl_object);
    var success: gl.int = undefined;
    gl.GetShaderiv(self.gl_object, gl.COMPILE_STATUS, &success);
    if (success == 0) {
        var info_log = std.mem.zeroes([256]u8);
        gl.GetShaderInfoLog(self.gl_object, 256, null, &info_log);
        return info_log;
    }
    return null;
}

pub fn delete(self: Shader) void {
    ...
}
```
❌
❌