Scicare
Scicare

Reputation: 843

project doesn't build correctly? or is it me?

I use Netbeans IDE, some where in the code I included this

ImageIcon accept_icon = new ImageIcon("src/images/accept.png");

When I run the project inside the IDE it display fine. However, when I try to build the project, it seems like external sources that I specified (images, template files, etc...) does not get included. Is it because how I specified the source (e.g src/images/accept.png ) ? If it is, how should I solve this problem? Sorry to ask such a simple question, Im quite new to java.

Upvotes: 0

Views: 77

Answers (5)

Ravi Shenoy
Ravi Shenoy

Reputation: 467

Try to unpack the jar and see if the image exists. If it doesnt, look for proper steps of packaging the application

Upvotes: 1

JLund
JLund

Reputation: 516

Consider following the example on the tutorial page and load it relative to your class as follows:

The following two excerpts are from http://download.oracle.com/javase/tutorial/uiswing/components/icon.html:

/** Returns an ImageIcon, or null if the path was invalid. */
protected ImageIcon createImageIcon(String path,
                                    String description) {
    java.net.URL imgURL = getClass().getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL, description);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}

In the preceding snippet, the first argument to the ImageIcon constructor is relative to the location of the current class, and will be resolved to an absolute URL. The description argument is a string that allows assistive technologies to help a visually impaired user understand what information the icon conveys.


Is your class in the default package? Try putting the image in a subfolder directly below your current class and use

ImageIcon accept_icon = new ImageIcon("images/accept.png");

Upvotes: 1

Michael Berry
Michael Berry

Reputation: 72344

If you're talking about building a jar and then running it then you have to embed the resources inside the jar explicitly if you want them there, and use the getResourceAsStream() method to use them.

See here for more information.

Upvotes: 1

Axel Fontaine
Axel Fontaine

Reputation: 35169

Load your resource from the classpath instead of the file system.

This way you won't be dependent on the current directory, absolute paths, ...

Upvotes: 2

Bernard
Bernard

Reputation: 7961

Seems like you're using a relative path to your image file. Trying making it a full (absolute) path (e.g. "/root/src/images/accept.png") and see if that resolves your issue.

Upvotes: 0

Related Questions