manifest file problem
hello, I made a simple game. my problem is that, netbeans automatically generate a jar file and manifest file, it also points main class correctly but my game involves some jpg files in folder named sprite, I also load them in code like this img.load("sprites/abc.jpg") however when I run jar file, java pops up errors that says it fails loading those resources, to overcome the problem I simply add my sprite folder in to the jar but java doesn't find them, what should I do ?
thank you for your help.
I created a java application, and a main class file, I also used resources in a folder called sprites then I build project, netbeans generated a jar file however when I run the jar, java cannot locate resources, even though sprite folder is near the jar file. It is not working even if I add sprite folder as a packge to the project ( this adds sprites to jar file), so how can I make an executable program (a jar file) that can access files in other folders ?
code is like this
_img = ImageIO.read(new File(fileName));
fileName = "sprites/abc.png"
code works without any problem through ide, problem occures when I run jar file
It's reasonable your approach doesn't work: there is no File called "sprites/abc.png" on the file system, how should ImageIO find it?
You have to work with resources... MyObject.class.getResource ("sprites/abc.png") kind of syntax, but then, you have to set up your directory structure just right... take a look here
thank you, I changed the code and make it something like this
URL url = getClass().getResource(fileName);
_img = new ImageIcon(Toolkit.getDefaultToolkit().getImage(url ));
now program is working as jar package too, but I still wonder if there is a way to open files through I/O class when program is being ran as jar package
_img = ImageIO.read(new File("Sprite/asd.jpg"));
or do I have to enter an absolute path ? like c/:...Game/Spirte/asd.jpg
The cleanest way to do this is to include all resources in the jar file, rather than trying to mix and match between the jar and contents elsewhere on the file system.
If there is some valid reason that you don't want to put al your resources in the jar file, then you need to come up with some other way to find external resources. The two common ways of doing that are to have an installer define them in preferences, or to discover the location of the jar file and use a relative path.
The second approach get's complicated if the jar is not an actual file, but a URL -...which is why you should include all resources in the jar file unless there is a compelling reason not to.
I see, I solve the problem, thank you for your advices.