How to load images,audio from folder in Project to work in .jar file

56 views Asked by At

I have in project extra folder called "res" which i set as an class folder. Evererything works fine when i run it in eclipse but wher i export it as jar file it doesn't there is problem with null pointer as if it can't see my images ,audio. I put Image for example in one class way: In some method of this class i just point a path

img = new ImageIcon("res\\"+BACKGROUND[CurrentLevel]+".jpg").getImage();

and then in paint component draw it

public void paintComponent(Graphics g)
{        
        g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null);   

}

My question is how to show this .jar file that all source should be find in "res" folder ?

1

There are 1 answers

0
Alex Rogachevsky On

Eclipse is not going to assemble the module (JAR/WAR) properly automatically. It only does it in debug mode. That's what build scripts (Ant and Maven) do. Ant is ancient, so don't even bother. Maven is the standard now. The easiest way to make everything right is to create a Maven project from scratch (in Eclipse). BTW IntelliJ is more Maven-centric, than Eclipse, so you wouldn't have that problem in the first place. Maven has standard folder convention, so anything in "/src/main/resources" is automatically copied to the JAR's (or WAR's) class folder.