无法使用gradle运行lwjgl项目

时间:2015-11-18 18:33:09

标签: java gradle lwjgl

这是我的build.gradle

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'

mainClassName = 'org.game.geom.EntryPoint'

run {
    jvmArgs '-Djava.library.path=resources//native//'
}

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.lwjgl:lwjgl:3.0.0a'
}

我有main

package org.game.geom;

import org.game.geom.graphics.GameWindow;

public class EntryPoint {
    public static void main(String[] args) {
        new GameWindow().run();
    }
}

我有GameWindow课程

package org.game.geom.graphics;

import static org.lwjgl.glfw.Callbacks.errorCallbackPrint;
import static org.lwjgl.glfw.Callbacks.glfwSetCallback;
import static org.lwjgl.glfw.GLFW.GLFWWindowSizeCallback;
import static org.lwjgl.glfw.GLFW.GLFW_RESIZABLE;
import static org.lwjgl.glfw.GLFW.GLFW_VISIBLE;
import static org.lwjgl.glfw.GLFW.glfwCreateWindow;
import static org.lwjgl.glfw.GLFW.glfwDefaultWindowHints;
import static org.lwjgl.glfw.GLFW.glfwGetPrimaryMonitor;
import static org.lwjgl.glfw.GLFW.glfwGetVideoMode;
import static org.lwjgl.glfw.GLFW.glfwInit;
import static org.lwjgl.glfw.GLFW.glfwMakeContextCurrent;
import static org.lwjgl.glfw.GLFW.glfwPollEvents;
import static org.lwjgl.glfw.GLFW.glfwSetErrorCallback;
import static org.lwjgl.glfw.GLFW.glfwSetWindowPos;
import static org.lwjgl.glfw.GLFW.glfwShowWindow;
import static org.lwjgl.glfw.GLFW.glfwSwapBuffers;
import static org.lwjgl.glfw.GLFW.glfwSwapInterval;
import static org.lwjgl.glfw.GLFW.glfwTerminate;
import static org.lwjgl.glfw.GLFW.glfwWindowHint;
import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose;

import java.nio.ByteBuffer;

import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWWindowSizeCallback.SAM;
import org.lwjgl.glfw.GLFWvidmode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GLContext;
import org.lwjgl.system.MemoryUtil;

public class GameWindow {

    private GLFWErrorCallback errorCallback;
    private long window;

    // move to another class
    public static final int DEFAULT_WIDTH = 600;
    public static final int DEFAULT_HEIGHT = 600;
    public static final boolean ALLOW_RESIZING = false;
    public static final int V_SYNC_ENABLED = 1;

    private int w_width;
    private int w_height;
    private boolean resized;

    private static final float[] CLEAR_COLOR = { 0.0f, 0.0f, 0.5f, 1.0f };
    private static final String WINDOW_TITLE = "Geom";

    public void run(int width, int height) {
        try {
            this.w_width = width;
            this.w_height = height;

            glfwSetErrorCallback(errorCallback = errorCallbackPrint(System.err));
            if (glfwInit() == GL11.GL_TRUE) {
                throw new IllegalStateException("Unable to initialize GLFW");
            }
            glfwDefaultWindowHints();
            glfwWindowHint(GLFW_VISIBLE, GL11.GL_FALSE);
            glfwWindowHint(GLFW_RESIZABLE, GL11.GL_TRUE);

            window = glfwCreateWindow(width, height, WINDOW_TITLE, MemoryUtil.NULL, MemoryUtil.NULL);
            if (window == MemoryUtil.NULL) {
                throw new RuntimeException("Failed to create GLFW window");
            }

            ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
            glfwSetWindowPos(window, getWidth(vidmode, width), getHeight(vidmode, width));
            glfwSetCallback(window, GLFWWindowSizeCallback(new SAM() {

                @Override
                public void invoke(long window, int width, int height) {
                    w_width = width;
                    w_height = height;
                    resized = true;
                }

            }));
            glfwMakeContextCurrent(window);
            glfwSwapInterval(V_SYNC_ENABLED);

            glfwShowWindow(window);
            GLContext.createFromCurrent();

            GL11.glClearColor(CLEAR_COLOR[0], CLEAR_COLOR[1], CLEAR_COLOR[2], CLEAR_COLOR[3]);

            while (glfwWindowShouldClose(window) == GL11.GL_FALSE) {
                if (resized) {
                    GL11.glViewport(0, 0, w_width, w_height);
                    resized = false;
                }
                GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
                glfwSwapBuffers(window); // double buffering

                glfwPollEvents();
            }

        } finally {
            glfwTerminate(); 
            errorCallback.release();
        }
    }

    public void run() {
        run(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }

    private int getWidth(ByteBuffer vidmode, int width) {
        return GLFWvidmode.width(vidmode) - width / 2;
    }

    private int getHeight(ByteBuffer vidmode, int height) {
        return GLFWvidmode.height(vidmode) - height / 2;
    }
}

当我尝试gradle run时出现此错误

Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize
class org.lwjgl.Sys
        at org.lwjgl.LWJGLUtil.initialize(LWJGLUtil.java:309)
        at org.lwjgl.glfw.GLFW.<clinit>(GLFW.java:395)
        at org.game.geom.graphics.GameWindow.run(GameWindow.java:101)
        at org.game.geom.graphics.GameWindow.run(GameWindow.java:107)
        at org.game.geom.EntryPoint.main(EntryPoint.java:7)

似乎无法找到native个库。 我将它们放入src/main/resources/native

如何解决?

0 个答案:

没有答案