I have an application that works fine with the Netbeans IDE, but when I run from the jar file, the necessary images are not loaded in the dist directory.
I spent 1 1/2 days reading this and other forums trying to find the answer, but I can't get jar images to work.
Here it is an extract from my 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);
}
I have tried many combinations of the relative path, including "images /", "/ images /", etc. Images are in the jar file:
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
etc...
I know that I am missing something fundamental, but I'm not sure what. My suspicion is that this has something to do with the manifest file, or possibly the class path.
Hope someone can point me in the right direction ...
EDIT: The problem is that
URL url = Terrain.class.getResource("/fantasyhexwar/Images/" + str);
null. , , JAR, , :
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");
JAR...
, :
java -cp.; FantasyHexWar.jar FantasyHexWarApp
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.