Question

I've got an issue with my .jar file. It runs fine in Eclipse but as soon as I export it, it won't open. I've checked the manifest file and it looks like it's okay.

I've tried exporting it as a runnable jar, as well as just using the jar builder. Nothing worked.

I've tried to run it in command prompt and it says it can't access the jar file... I've searched around a while on here and haven't found an answer yet.

I'm not sure what I'm doing wrong. The only thing I can think of is I'm not getting my images correctly.

I'm using .png files for the program's sprites and here's an example of how I get them for my program.

This code begins the building of a level from a .png file.

public class SpawnLevel extends Level{

public SpawnLevel(String path) {
    super(path);
}

protected void loadLevel(String path){
    try{
        System.out.println("classpath is: " + System.getProperty("java.class.path"));
        BufferedImage image = ImageIO.read(SpawnLevel.class.getResource(path));
        int w = width = image.getWidth();
        int h = height= image.getHeight();
        tiles = new int[w*h];
        image.getRGB(0,0,w,h,tiles,0,w);
    }catch(IOException e){
        e.printStackTrace();
        System.out.println("EXEPTION FOUND!!!  Could not load the level file!");
    }
}

protected void generateLevel(){
    System.out.println("Tiles: " + tiles[0]);
}

}

I've made one other .jar before for another program and didn't have a problem. Any help would be greatly appreciated.

If it helps, I used this code to display the resource folder path information.

System.out.println("classpath is: " + System.getProperty("java.class.path"));

Here's what my current path for my resources folder looks like. Before I export it from eclipse.

    classpath is: C:\Users\name\workspace\Rpg_Game\bin;C:\Users\name\workspace\Rpg_Game\res

After I export to .jar

    classpath is: 2ndGameTest.jar
Was it helpful?

Solution

If your images are in your resources package in the src The path you should be using for getResource() is something like

class.getResource("/resources/levels/level1.png")

UPDATE with test program

import java.awt.Image;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class TestImage {

    public static void main(String[] args) throws IOException {
        Image image = ImageIO.read(TestImage.class.getResource("/resources/images/image.png"));
        ImageIcon icon = new ImageIcon(image);
        JLabel label = new JLabel(icon);
        JOptionPane.showMessageDialog(null, label);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top