Eclipse:线程“Game”中的异常java.lang.IllegalStateException:请使用-XstartOnFirstThread运行JVM

时间:2015-04-03 21:02:38

标签: java eclipse

我正在考虑如何在日食(java)上制作飞鸟。tutorial。我按照此视频的所有说明进行了操作,但是它说错误:

Exception in thread "Game" java.lang.IllegalStateException: Please run the JVM with -XstartOnFirstThread.
at org.lwjgl.system.macosx.EventLoop.checkFirstThread(EventLoop.java:20)
at org.lwjgl.glfw.GLFW.glfwInit(GLFW.java:426)
at com.thecherno.flappy.Main.init(Main.java:37)
at com.thecherno.flappy.Main.run(Main.java:79)
at java.lang.Thread.run(Thread.java:745)

文件位置正确,但出现错误。这是Main类代码:

package com.thecherno.flappy;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL13.*;
import static org.lwjgl.system.MemoryUtil.*;
import java.nio.ByteBuffer;
import org.lwjgl.glfw.GLFWvidmode;
import org.lwjgl.opengl.GLContext;
import com.thecherno.flappy.graphics.Shader;
import com.thecherno.flappy.input.Input;
import com.thecherno.flappy.level.Level;
import com.thecherno.flappy.maths.Matrix4f;

public class Main implements Runnable {

private int width = 1280;
private int height = 720;

private Thread thread;
private boolean running = false;

private long window;

private Level level;

public void start() {
    running = true;
    thread = new Thread(this, "Game");
    thread.start();
}

private void init() {
    if (glfwInit() != GL_TRUE) {
        System.err.println("Could not initialize GLFW!");
        return;
    }

    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
    window = glfwCreateWindow(width, height, "Flappy", NULL, NULL);
    if (window == NULL) {
        System.err.println("Could not create GLFW window!");
        return;
    }

    ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, (GLFWvidmode.width(vidmode) - width) / 2, (GLFWvidmode.height(vidmode) - height) / 2);

    glfwSetKeyCallback(window, new Input());

    glfwMakeContextCurrent(window);
    glfwShowWindow(window);
    GLContext.createFromCurrent();

    glEnable(GL_DEPTH_TEST);
    glActiveTexture(GL_TEXTURE1);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    System.out.println("OpenGL: " + glGetString(GL_VERSION));
    Shader.loadAll();

    Matrix4f pr_matrix = Matrix4f.orthographic(-10.0f, 10.0f, -10.0f * 9.0f / 16.0f, 10.0f * 9.0f / 16.0f, -1.0f, 1.0f);
    Shader.BG.setUniformMat4f("pr_matrix", pr_matrix);
    Shader.BG.setUniform1i("tex", 1);

    Shader.BIRD.setUniformMat4f("pr_matrix", pr_matrix);
    Shader.BIRD.setUniform1i("tex", 1);

    Shader.PIPE.setUniformMat4f("pr_matrix", pr_matrix);
    Shader.PIPE.setUniform1i("tex", 1);

    level = new Level();
}

public void run() {
    init();

    long lastTime = System.nanoTime();
    double delta = 0.0;
    double ns = 1000000000.0 / 60.0;
    long timer = System.currentTimeMillis();
    int updates = 0;
    int frames = 0;
    while (running) {
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        if (delta >= 1.0) {
            update();
            updates++;
            delta--;
        }
        render();
        frames++;
        if (System.currentTimeMillis() - timer > 1000) {
            timer += 1000;
            System.out.println(updates + " ups, " + frames + " fps");
            updates = 0;
            frames = 0;
        }
        if (glfwWindowShouldClose(window) == GL_TRUE)
            running = false;
    }

    glfwDestroyWindow(window);
    glfwTerminate();
}

private void update() {
    glfwPollEvents();
    level.update();
    if (level.isGameOver()) {
        level = new Level();
    }
}

private void render() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    level.render();

    int error = glGetError();
    if (error != GL_NO_ERROR)
        System.out.println(error);

    glfwSwapBuffers(window);
}

public static void main(String[] args) {
    new Main().start();
}

}

感谢您的帮助,我也是新的堆栈溢出。如果你在15岁之前提高我的声誉,我会很高兴,所以我可以提高其他声誉。

谢谢,

-Mjl

2 个答案:

答案 0 :(得分:2)

这可能与this SWT issue on Mac OS X有关。如错误消息所示,请尝试使用-XstartOnFirstThread

运行
java -XstartOnFirstThread Main

在Eclipse中,转到运行 - >运行配置... 在用于启动应用程序的运行配置中,在参数选项卡下,在 VM参数中添加-XstartOnFirstThread部分,然后运行应用程序。

答案 1 :(得分:1)

尝试将main方法更改为以下内容:

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