开发者

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 s开发者_Go百科olve this problem? Sorry to ask such a simple question, Im quite new to java.


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

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


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.


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");


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜