Load images from a jar file
<p>I have an application that works perfectly from the Netbeans IDE, but when run from the jar file in the dist directory does not load the necessary images.</p>

<p>I have spent 1 1/2 days reading this and other forums, trying to find an answer, but I can't get the jar images to work.</p>

<p>Here he is an extract from my code:</p>

<pre><code>String str = t.getText() + "100.gif";
Image img = null;

if (t != HIDDEN)
{
    ClassLoader cldr = Terrain.class.getClassLoader();
    URL url = cldr.getResource("fantasyhexwar/Images/" + str);

    if (url == null)
        JOptionPane.showMessageDialog(null, "no good");

    img = ImageIO.read(url);
    t.setImage(img);
}
</code></pre>

<p>I have tried many combinations of relative path, including "images/", "/images/", etc. The images are in the jar file:</p>

<pre><code> fantasyhexwar/Images/plains100.gif
 fantasyhexwar/Images/quarry60.gif
 fantasyhexwar/Images/ram80.gif
 fantasyhexwar/Images/save map40.gif
 fantasyhexwar/Images/scout80.gif
 fantasyhexwar/Images/settler80.gif
 fantasyhexwar/Images/ship80.gif
</code></pre>

<p>etc...</p>

<p>I know I am missing something fundamental, but I'm not sure what. My suspicion is that it is something to do with the manifest file or possibly class path.</p>

<p>Hopefully someone can point me in the right direction...</p>

<p>EDIT:
The problem seems to be that </p>

<pre><code>URL url = Terrain.class.getResource("/fantasyhexwar/Images/" + str);
</code></pre>

<p>returns null. The images are definitely in the JAR, and in desperation I have also tried all possible relative paths, with code like this:</p>

<pre><code>ClassLoader cldr = Terrain.class.getClassLoader();
URL url = Terrain.class.getResource("/fantasyhexwar/Images/" + str);
if (url == null)
    url = cldr.getResource("/fantasyhexwar/fantasyhexwar/Images/" + str);
if (url == null)
    url = cldr.getResource("fantasyhexwar/fantasyhexwar/Images/" + str);
if (url == null)
    url = cldr.getResource("/fantasyhexwar/Images/" + str);
if (url == null)
    url = cldr.getResource("/Images/" + str);
if (url == null)
    url = cldr.getResource("Images/" + str);
if (url == null)
    url = cldr.getResource("/" + str);
if (url == null)
    url = cldr.getResource(str);
if (url == null)
    JOptionPane.showMessageDialog(null, "no good");
</code></pre>

<p>But none of it works when executing directly from the JAR...</p>

<p>When I try to run from the command line, I get:</p>

<blockquote>
  <p>java -cp .;FantasyHexWar.jar FantasyHexWarApp</p>
</blockquote>

<pre><code>Exception in thread "main" java.lang.NoClassDefFoundError: FantasyHexWarApp
Caused by: java.lang.ClassNotFoundException: FantasyHexWarApp
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: FantasyHexWarApp.  Program will exit.
</code></pre>
