0

I have been having a problem with this all day.

My program currently is a simple client server application using Java's RMI.

Everything here is happening client side.

I have a folder in my project folder under src, called resources, which contains all the images and other files

To get an image I am using:

public static final Image SPY_LARGE_ICON = 
    Toolkit.getDefaultToolkit().getImage(Constants.RESOURCES_LOCATION + "spy.png");

This is done in my Constants class as it is used all over my program.

Now for the problem, while running in Eclipse it is working fine. The image(s) appear as desired all over the application. However when I export it to a runnable JAR, it refuses to see the location.

The RESOURCES_LOCATION const is currently set as:

     public static final String RESOURCES_LOCATION = ".\\src\\Resources\\";

I have tried, with and without the dot, using / instead of \ and a mixture of both.

I have also messed around with the class path a lot, however to no avail.

But I am still unable to access the file when it is exported as a JAR.

I am out of ideas, of what the path could be to the image.

Thanks Adam

Edit: It didn't make a difference if the file was included as a source folder in the build path

Answer:

       public static Image getImageFromFileOrJar(Class classs, StringfileName) {
    Image image;
    if (isDEBUG) {
        image = Toolkit.getDefaultToolkit().getImage(
                Constants.RESOURCES_LOCATION + fileName);
    } else {
        image = Toolkit.getDefaultToolkit()
                .getImage(
                        classs.getResource(Constants.RESOURCES_LOCATION
                                + fileName));
    }
    return image;
}

As when using debuging the program through eclipse the jar has not been built yet, therefore it cannot see it. So I put in a boolean value to change once the project goes live.

icedwater
  • 4,701
  • 3
  • 35
  • 50

0 Answers0