javanetbeansjarresource-loading

Packing a .jar file with external files


I've recently finished a project I'm working on, and part of it involves using ImageIcons to fill JLabels. So my question is about how I would go about making the filepaths more universal. By that I mean rather that having the path to my image being something like "C:/users/user/documents/project/file.jpg" for example. I'm looking to pack it into a .jar file and include the target folders/files in a zip with the jar.


Solution

  • In the Using Swing Components tutorial the Loading Images Using getResource section shows seven scenarios in which the following code should works:

    java.net.URL imageURL = myDemo.class.getResource("images/myImage.gif");
    ...
    if (imageURL != null) {
        ImageIcon icon = new ImageIcon(imageURL);
    }