I have the next directory structure for my personal project:
mines/
build.xml
build/
build/classes/
build/jar/
src/minesweeper
src/minesweeper/view/
src/minesweeper/model/
res/img/
and my build.xml file is:
<project>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/minesweeper-clone.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="minesweeper.Minesweeper"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/minesweeper-clone.jar" fork="true"/>
</target>
In Ubuntu I do:
$ant compile jar run
and everything is ok. But if I do:
$java -jar minesweeper.jar
the program runs, but the icons are not displayed.
In my src/minesweeper/view/GameFrame.java class I have:
private static final String NEW_GAME_ICON = "res/img/new_game.png";
and new_game.png is in res/img/new_game.png
I know if I do:
$java -jar build/jar/minesweeper-clone.jar
it works perfect and the icons are displayed, but I want to execute only the jar file in windows (without the whole directory of my project).
Thanks.
EDIT
The way I access the resources is:
JMenuItem newGameItem1 = new JMenuItem("New game", new ImageIcon(NEW_GAME_ICON));
You want to include the resource into the jar itself, and read the file using this API:
getResourceAsStream
So first you need to make sure the resource is in the jar (open it up and make sure it’s there), and two make sure you are using an API call such as that and not File IO mechanisms (because the image will be in the jar; not on the filesystem).