I have "-XstartOnFirstThread" in my VM arguments, however I am still getting the error message:
Exception in thread "Thread-0" java.lang.ExceptionInInitializerError
at org.lwjgl.glfw.GLFW.glfwCreateWindow(GLFW.java:1248)
at Main.init(Main.java:33)
at Main.run(Main.java:56)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: GLFW windows may only be created on the main thread.
at org.lwjgl.glfw.EventLoop$OffScreen.<clinit>(EventLoop.java:39)
... 4 more
My Code:
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;
import org.lwjgl.glfw.*;
public class Main implements Runnable {
private Thread thread;
private boolean running;
public long window;
public static void main(String[] args) {
Main game = new Main();
game.start();
}
public void start() {
running = true;
thread = new Thread(this);
thread.start();
}
public void init() {
if(glfwInit() != GL_TRUE) {
System.err.println("GLFW Initialization Failed!");
}
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
window = glfwCreateWindow(800, 600, "test", NULL, NULL);
if(window == NULL) {
System.err.println("Could not create our window!");
}
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, 100, 100);
glfwMakeContextCurrent(window);
glfwShowWindow(window);
}
public void update() {
glfwPollEvents();
}
public void render() {
glfwSwapBuffers(window);
}
public void run() {
init();
while(running) {
update();
render();
if(glfwWindowShouldClose(window) == GL_TRUE) {
running = false;
}
}
}
}
I am asking this question here because I have looked around and have not seen a solution posted for this anywhere else. Thank you for your help!
Right now you are starting your program in the main thread, but immediately creating a new thread that creates the window. In LWJGL, you should do all GLFW calls and OpenGL rendering in the main thread. You can use other threads to build VBOs, load textures, calculate physics, etc.