【问题标题】:File not found after creating a runnable jar using jarsplice使用 jarsplice 创建可运行的 jar 后找不到文件
【发布时间】:2013-09-09 07:40:29
【问题描述】:

以下是 CMD 在使用 JarSplice 将 LWJGL 编译为可运行 Jar 后告诉我的内容。

C:\Users\Rose>java -jar C:\Users\Rose\Desktop\Test\FrikFatJar.jar
Error: java.io.FileNotFoundException: res\350.png (The system cannot find the pa
th specified)
loading glyphs -1
glyphs loaded
loading glyphs -1
glyphs loaded
Exception in thread "main" java.lang.NullPointerException
        at frik.entity.Image.draw(Image.java:164)
        at frik.screen.Menu.draw(Menu.java:44)
        at frik.main.FrikMolder.updateContent(FrikMolder.java:77)
        at frik.main.FrikMolder.isRunning(FrikMolder.java:54)
        at frik.main.MainClass.<init>(MainClass.java:36)
        at frik.main.MainClass.main(MainClass.java:74)

我追溯了这个问题,它与使用java.io.File而不是InputStream()有关,但问题是,我使用的是InputStream()。

这是来自我的 Image 类。

public void init() {


    try{
        texture = TextureLoader.getTexture(textForm, new FileInputStream(textName), true);
    }catch(IOException e){
        System.out.println("Error: "+e);
    }
    rotation = 0;
    if(width != height&&width % 2 != 0&&height % 2 != 0){
        textForm = "PNG";
        textName = "res/128.png";
        JOptionPane.showMessageDialog(null, "The width and height of the Image should be equal.", "Wrong image size!", JOptionPane.WARNING_MESSAGE);
        init();
    }
    timer = new Timer();
}

public void draw() {
    if(ExtraBind){          
        glBindTexture(GL_TEXTURE_2D, texture.getTextureID());
    }else{
        texture.bind();
    }
    glColor4f(c4f[0], c4f[1], c4f[2], c4f[3]);;

    glPushMatrix();

    glTranslatef(x + width /2, y + height /2, 0);
    glRotatef(rotation, 0, 0, 1f);
    glTranslatef(-x - width /2, -y - height /2, 0);

    glBegin(GL_QUADS);

        glTexCoord2f(0,0);
        glVertex2f(x, y);

        glTexCoord2f(1,0);
        glVertex2f(x + width,y);

        glTexCoord2f(1,1);
        glVertex2f(x + width, y + height);


        glTexCoord2f(0,1);
        glVertex2f(x, y + height);

    glEnd();

    glPopMatrix();
    glBindTexture(GL_TEXTURE_2D, 0);
}

这是我试图从菜单类加载的图像

IMG = new Image("PNG", "res/350.png", 0,0);

感谢您的宝贵时间。

【问题讨论】:

  • 你能再解释一下吗?

标签: java jar textures inputstream lwjgl


【解决方案1】:

我修好了,这是我在init() 中更改的内容

public void init() {


    try{    
        if(Special == true){

            texture = TextureLoader.getTexture(textForm, loadBufferedImage(textName), true);
        }else{
            texture = TextureLoader.getTexture(textForm, new FileInputStream(textName), true);
        }
    }catch(IOException e){
        System.out.println("Error: "+e);
    }
    Special = true;
    rotation = 0;
    if(width != height&&width % 2 != 0&&height % 2 != 0){
        textForm = "PNG";
        textName = "/img/128.png";
        Special = false;
        JOptionPane.showMessageDialog(null, "The width and height of the Image should be equal.", "Wrong image size!", JOptionPane.WARNING_MESSAGE);
        init();
    }

我添加了这个从 jar 文件加载的方法。

private InputStream loadBufferedImage(String string)
{
    try
    {
        BufferedImage bi = ImageIO.read(this.getClass().getResource(string));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write( bi, "PNG", baos);
        InputStream is = new ByteArrayInputStream(baos.toByteArray());
        return is;
    } catch (IOException e)
    {
        System.out.println("Error: "+e);
    }
    return null;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-09
    • 2013-04-12
    • 2012-12-10
    • 1970-01-01
    • 2013-01-16
    • 2014-04-01
    • 2015-03-01
    • 1970-01-01
    相关资源
    最近更新 更多