【问题标题】:LWJGL executable jar starts then crashesLWJGL 可执行 jar 启动然后崩溃
【发布时间】:2012-05-28 23:22:43
【问题描述】:

我希望得到一些帮助。我为读取四个 .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;
    }
}

【问题讨论】:

    标签: jar crash lwjgl


    【解决方案1】:

    打开命令提示符,输入:

    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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-12
      • 2017-12-13
      • 2012-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多