LWJGL可执行jar启动然后崩溃

时间:2012-05-28 23:22:44

标签: jar crash lwjgl

我希望得到一些帮助。我为类开发了这个程序,它读取了四个.obj文件和一个纹理文件。它在我的IDE(Eclipse)中工作正常但是当我导出它并使用JavaSplice创建最终的可执行jar时,它会启动,显示一个显示窗口几分之一秒,然后崩溃。通过一些测试和仔细评论,我已经确定它不是在拼接结束而是我的代码。通过评论,我已经确定问题在于在四个.obj中读取的显示列表:

int objectOneDisplayList = glGenLists(1);
glNewList(objectOneDisplayList, GL_COMPILE);
{   
        Model m = null;
        try
        {
            m = OBJLoader.loadModel(new File("res/circle.obj"));
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
            Display.destroy();
        }
        catch(IOException e)
        {
            e.printStackTrace();
            Display.destroy();
        }

        glBegin(GL_TRIANGLES);
        for(Face face : m.faces)
        {
            glColor3f(0.0f, 0.75f, 0.0f);
            Vector3f n1 = m.normals.get((int) face.normal.x - 1);
            glNormal3f(n1.x, n1.y, n1.z);
            Vector3f v1 = m.vertices.get((int) face.vertex.x - 1);
            glVertex3f(v1.x, v1.y, v1.z);
            Vector3f n2 = m.normals.get((int) face.normal.y - 1);
            glNormal3f(n2.x, n2.y, n2.z);
            Vector3f v2 = m.vertices.get((int) face.vertex.y - 1);
            glVertex3f(v2.x, v2.y, v2.z);
            Vector3f n3 = m.normals.get((int) face.normal.z - 1);
            glNormal3f(n3.x, n3.y, n3.z);
            Vector3f v3 = m.vertices.get((int) face.vertex.z - 1);
            glVertex3f(v3.x, v3.y, v3.z);
        }
        glEnd();
    }
    glEndList();

其中有四个只有不同的文件名和列表名称。现在这是我的OBJLoader类:

package base;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.lwjgl.util.vector.Vector3f;


public class OBJLoader
{
    public static Model loadModel(File f) throws FileNotFoundException, IOException 
    {
        BufferedReader reader = new BufferedReader(new FileReader(f));
        Model m = new Model();
        String line;
       while((line = reader.readLine()) != null)
       {
            if(line.startsWith("v "))
            {
                float x = Float.valueOf(line.split(" ")[1]);
                float y = Float.valueOf(line.split(" ")[2]);
                float z = Float.valueOf(line.split(" ")[3]);
                m.vertices.add(new Vector3f(x, y, z));
            }
            else if(line.startsWith("vn "))
            {
                float x = Float.valueOf(line.split(" ")[1]);
                float y = Float.valueOf(line.split(" ")[2]);
                float z = Float.valueOf(line.split(" ")[3]);
                m.normals.add(new Vector3f(x, y, z));
            }
            else if(line.startsWith("f "))
            {
                Vector3f vertexIndices = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[0]),
                Float.valueOf(line.split(" ")[2].split("/")[0]),
                Float.valueOf(line.split(" ")[3].split("/")[0]));
                Vector3f normalIndices = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[2]),
                Float.valueOf(line.split(" ")[2].split("/")[2]),
                Float.valueOf(line.split(" ")[3].split("/")[2]));

                m.faces.add(new Face(vertexIndices, normalIndices));
            }
        }
        reader.close();
        return m;
    }
}

我认为这是因为我需要使用InputFileStream。有人能告诉我如何使用它重写这个吗?

编辑:重写OBJLoader以不使用FileReader,仍然无法正常工作。我想我不能使用任何类型的读者。那我怎么读这些线?:

public class OBJLoader
{
    public static Model loadModel(File f) throws FileNotFoundException, IOException 
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
        Model m = new Model();
        String line;
        while((line = reader.readLine()) != null)
        {
            if(line.startsWith("v "))
            {
                float x = Float.valueOf(line.split(" ")[1]);
                float y = Float.valueOf(line.split(" ")[2]);
                float z = Float.valueOf(line.split(" ")[3]);
                m.vertices.add(new Vector3f(x, y, z));
            }
            else if(line.startsWith("vn "))
            {
                float x = Float.valueOf(line.split(" ")[1]);
                float y = Float.valueOf(line.split(" ")[2]);
                float z = Float.valueOf(line.split(" ")[3]);
                m.normals.add(new Vector3f(x, y, z));
            }
            else if(line.startsWith("f "))
            {
                Vector3f vertexIndices = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[0]),
                        Float.valueOf(line.split(" ")[2].split("/")[0]),
                        Float.valueOf(line.split(" ")[3].split("/")[0]));
                Vector3f normalIndices = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[2]),
                        Float.valueOf(line.split(" ")[2].split("/")[2]),
                        Float.valueOf(line.split(" ")[3].split("/")[2]));

                m.faces.add(new Face(vertexIndices, normalIndices));
            }
        }
        reader.close();
        return m;
    }
}

1 个答案:

答案 0 :(得分:0)

打开命令提示符,键入:

java -jar "pathToYourJar.jar"

通过这个,您可以看到代码抛出的异常。

关于崩溃,我认为可以使用InputStreams而不是Files来解决问题。

变化:

BufferedReader reader = new BufferedReader(new InputStreamReader
    (new FileInputStream(f)));

String path = "/package1/package2/file.obj";
InputStream source = ClassLoader.class.getResourceAsStream(path);
BufferedReader reader = new BufferedReader(new InputStreamReader
    (source);

这将在Eclipse和JAR中发挥作用。

相关问题