0

Problem is the following - I am writing a program with Eclipse in java.. The resulting folder structure is main project folder: Animal Classification/ inside of it: bin/ , images/, pics/ in the bin/ folder : users/metropolia/allClasses.class

Program starts normally and works -> images are in the folders images/ and pics/ -> they are used by users.metropolia.Hieararchy

But when I create a .jar out of it either through Eclipse or through CMD.exe -> in the folder different from the source folder the jar does not show images even though they are packed inside of it. Inside the Hierarchy.class the path to the images is ("images/name.jpg" or "pics/name.jpg").

Then I tried to use the ClassLoader.getSystemResource("images/name.jpg") - but now the eclipse and the cmd.exe give warnings and dont start the program -> NullPointerException in thread main = mistake in the line where ClassLoader static is used...

I would really appreciate any help

Update:

http://users.metropolia.fi/~artemm/

Download the code from there plz It can be downloaded as SFX-archive or if you are afraid of viruses just download usual RAR archive

I really appreciate your help

Main class is in users/metropolia/Program.java

Update 2:

@MockerTim

THank you very much for your help - now everything works. Moreover, I managed to find one more snag - it didnt call for the images, because the call was : "Fish.jpg" and the picture was : "fish.jpg" - no wonder it could not find anything and gave mistakes.

THank you very much once again, I really appreciate!

MockerTim
  • 2,475
  • 1
  • 25
  • 31
Artem Moskalev
  • 5,748
  • 11
  • 36
  • 55
  • 2
    What's the actual error message? In any case, I've always used `getResourceAsStream` for resources within a jar. – Dave Newton Oct 20 '11 at 12:44
  • 1
    @Artem Moskalev: If you use the result of `getSystemResource()` in combination with the `File` class, then you're out of luck with Jar files. The contents of a Jar file cannot be accessed with the `File` class. In addition to the actual error message, please also show more code (how you access the images). – Codo Oct 20 '11 at 12:48
  • possible duplicate of [Java Swing: Displaying images from within a Jar](http://stackoverflow.com/questions/31127/java-swing-displaying-images-from-within-a-jar) (or about a hundred other questions about loading images from JAR files.) – Nate Oct 20 '11 at 13:09
  • This is the snippet of Hierarchy.java...images are loaded in MyDrawPanel class and Hierarchy::start() method – Artem Moskalev Oct 20 '11 at 19:30
  • I failed to compile your code. Have you tried to compile this code before publishing it here? If you place any code, it must be [SSCCE](http://sscce.org). Otherwise it doesn't help to solve your problem. The full stack trace may also help. – MockerTim Oct 20 '11 at 20:18
  • THe problem is - the whole application contains much more code - is it possible to stick it as a file?? – Artem Moskalev Oct 20 '11 at 20:41
  • This is just the main class connected from the Program.class in its main method - it contains the ImageIcon("") constructors – Artem Moskalev Oct 20 '11 at 20:42
  • You can add code of all needed files. Just devide it by comments. See [my post](http://stackoverflow.com/questions/6000116/java-generics-wildcards-question/6027155#6027155), for example. It's hard to find the mistake in your code if we can't even compile it. You should modify your code to fit [SSCCE](http://sscce.org). – MockerTim Oct 20 '11 at 21:06
  • http://users.metropolia.fi/~artemm/ Here you can download the whole thing its my "web site" Main file - users.metropolia.Program.java – Artem Moskalev Oct 20 '11 at 21:59
  • http://users.metropolia.fi/~artemm/ Check my Code over there It is in archives RAR THe main class is Program.java in users/metropolia – Artem Moskalev Oct 20 '11 at 22:11
  • You are Welcome! But, Do not remove your initial question from your post. Let people see the initial question. Make updates to your question. Place your thanks in comments only. See the [FAQ]. – MockerTim Oct 21 '11 at 14:19

2 Answers2

2

You are using incorrect paths.

You should use the following paths in your code: ../../images/name.jpg and ../../pics/name.jgp, respectively.

As Nate mentioned, you can start you path with "/", and use /images/name.jpg and /pics/name.jpg, respectively.

When you use images/name.jpg path in the class users.metropolia.Hieararchy, your code tries to find file name.jpg in the folder users/metropolia/images. And, of course, it fails.

See Locating resources in Java for details.

Here is the simple example:

package users.metropolia;

import java.net.URL;
import javax.swing.ImageIcon;

public class IconManager {
    /** Returns an ImageIcon, or null if the path was invalid. */
    public static ImageIcon loadImageIcon(String path) {
        URL imgURL = IconManager.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
                return null;
        }
    }

    public static void main(String[] args) {
        // Using absolute (to jar) paths
        ImageIcon icon3 = loadImageIcon("/pics/spider.jpg");
        ImageIcon icon4 = loadImageIcon("/imagess/ant.jpg");
    }
}

Update:

I've downloaded your code (from your link), and added the above IconManager class and used its loadImageIcon() method to initialize your anImages array and MyDrawPanel class. Now your progam looks like this:

enter image description here

The changes, that I've made to your code:

In class Hierarchy:

for (int i = 0; i < options.length; i++) {
  //anImages[i] = new ImageIcon("/images/" + options[i] + ".jpg");
    anImages[i] = IconManager.loadImageIcon("/images/" + options[i] + ".jpg");
    listObj[i] = new RendObject(options[i], anImages[i]);
}

In class MyDrawPanel:

public MyDrawPanel(String x) {
  //picture = new ImageIcon(x).getImage();
    picture = IconManager.loadImageIcon(x).getImage();
}

And, of course, I removed "generic" from JComboBox where needed.

Remark: To make your program show selected image instead of spider image, you should modify your program further.

Community
  • 1
  • 1
MockerTim
  • 2,475
  • 1
  • 25
  • 31
0

To read a file when you create a Jar file in the META-INF folder you must create a 'INDEX.LIST' file '.LIST' is the extension of the file.

in this 'INDEX.LIST' you describe the path to your file