2015-04-03 93 views
0

我正在做一個tutorial關於如何在eclipse(java)上製作flappy bird。我按照這個視頻的所有說明,但它說這個錯誤:Eclipse:線程「Game」中的異常java.lang.IllegalStateException:請使用-XstartOnFirstThread運行JVM

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) 

該文件的位置是正確的,但有錯誤。這裏是主類代碼:

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

這可能與this SWT issue on Mac OS X。由於錯誤消息說,嘗試用-XstartOnFirstThread運行:

java -XstartOnFirstThread Main 

在Eclipse中,轉到運行 - >運行配置...在您用來啓動應用程序,根據參數運行配置標籤,在VM參數部分添加-XstartOnFirstThread部分,然後運行該應用程序。

+0

請更具體一點,我很Eclipse新手。我應該做些什麼?我該怎麼改變? – MjlDoesProgramming 2015-04-03 21:44:53

+0

@MjlDoesProgramming更新。 – manouti 2015-04-03 21:50:11

0

謝謝@manouti回答我的問題,但這裏的另一個錯誤:在+斷言失敗[NSUndoManager _endTopLevelGroupings]

以下是完整的錯誤:2015年4月7日16:37:44.617的java [22936: 81493] * [NSUndoManager _endTopLevelGroupings]中的斷言失敗,/SourceCache/Foundation/Foundation-1151.16/Misc.subproj/NSUndoManager.m:340 2015-04-07 16:37:44.619 java [22936:81493] + [ NSUndoManager(NSInternal)_endTopLevelGroupings]只能在主線程上調用。 2015年4月7日16:37:44.620的java [22936:81493]( 0的CoreFoundation 0x00007fff9515364c __exceptionPreprocess + 172 1周libobjc.A.dylib 0x00007fff916536de objc_exception_throw + 43 2的CoreFoundation 0x00007fff9515342a + [NSException提高:格式:參數:] + 106 3基礎0x00007fff948155b9 - [NSAssertionHandler handleFailureInMethod:對象:文件:LINENUMBER:描述:] + 195 4基金會0x00007fff94786fef + [NSUndoManager(NSPrivate)_endTopLevelGroupings] + 156 5了AppKit 0x00007fff927e9ec5 - [的NSApplication運行] + 756 6 liblwjgl .dylib 0x000000010a64a560 _glfwPlatformCreateWindow + 1392 7 liblwjgl.dylib 0x000000010a6468a5 glfwCreateWindow + 597 8 ??? 0x00000001030127f8 0x0 + 4345374712 9 ??? 0x0000000103006175 0x0 + 4345323893 10 ??? 0x0000000103006175爲0x0 + 4345323893 ) 2015年4月7日16:37:44.620的java [22936:81493] 在+ [NSUndoManager _endTopLevelGroupings]斷言故障,/SourceCache/Foundation/Foundation-1151.16/Misc.subproj/NSUndoManager.m :340 java [22936:81493]一個未捕獲的異常被提出 2015-04-07 16:37:44.625 java [22936:81493] + [NSUndoManager(NSInternal)_endTopLevelGroupings]是隻能安全地在主線程上調用。 2015-04-07 16:37:44.625 java [22936:81493]( 0 CoreFoundation 0x00007fff9515364c __exceptionPreprocess + 172 1 libobjc.A。dylib 0x00007fff916536de objc_exception_throw + 43 2的CoreFoundation 0x00007fff9515342a + [NSException提高:格式:參數:] + 106 3基礎0x00007fff948155b9 - [NSAssertionHandler handleFailureInMethod:對象:文件:LINENUMBER:描述:] + 195 4基金會0x00007fff94786fef + [NSUndoManager(NSPrivate )_endTopLevelGroupings] + 156 5了AppKit 0x00007fff927e9f71 - [運行的NSApplication] + 928 6 liblwjgl.dylib 0x000000010a64a560 _glfwPlatformCreateWindow + 1392 7 liblwjgl.dylib 0x000000010a6468a5 glfwCreateWindow + 597 8 ??? 0x00000001030127f8 0x0 + 4345374712 9 ??? 0x0000000103006175 0x0 + 4345323893 10 ??? 0x0000000103006175爲0x0 + 4345323893 ) 2015年4月7日16:37:44.626的java [22936:81493] 終止應用程序由於未捕獲的異常 'NSInternalInconsistencyException',原因:「+ [NSUndoManager(NSInternal)_endTopLevelGroupings】僅安全地在主線程上調用'。 *第一擲調用堆棧: ( 0的CoreFoundation 0x00007fff9515364c __exceptionPreprocess + 172 1周libobjc.A.dylib 0x00007fff916536de objc_exception_throw + 43 2的CoreFoundation 0x00007fff9515342a + [NSException提高:格式:參數:] + 106 3基礎0x00007fff948155b9 - [ NSAssertionHandler handleFailureInMethod:對象:文件:LINENUMBER:描述:] + 195 4基金會0x00007fff94786fef + [NSUndoManager(NSPrivate)_endTopLevelGroupings] + 156 5了AppKit 0x00007fff927e9f71 - [運行的NSApplication] + 928 6 liblwjgl.dylib 0x000000010a64a560 _glfwPlatformCreateWindow + 1392 7 liblwjgl.dylib 0x000000010a6468a5 glfwCreateWindow + 597 8 ??? 0x00000001030127f8 0x0 + 4345374712 9 ??? 0x0000000103006175 0x0 + 4345323893 10 ??? 0x0000000103006175爲0x0 + 4345323893 ) 的libC++ abi.dylib:與類型NSException的未捕獲的異常

1

嘗試改變的主要方法如下終止:

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