Java resource images not showing

I am trying to get graphical resources to display in the GUI that I am developing, but it is difficult for me to display them. I came across other questions about loading resources like java-swing-displaying-images-from-within-a-jar and images-will-not-work-in-a-jar-file , but they do not work for me.

My problem seems to coincide with the first link where the images appear when starting from Eclipse and do not appear when starting from the command line using Jars. However, the solution of these issues does not lead to the appearance of images.

Code for loading resources:

public class R {

    public static final String resourcePackage = "path/to/image/package";

    /**
     * Returns the resource string that refers to the <code>resource</code> file
     * in the <code>path.to.image.package.png</code> package.
     * 
     * @param resource
     *            the file in the <code>png</code> package
     * @return the full resource string
     */
    public static String png(String resource) {
        return String.format("%s/png/%s", resourcePackage, resource);
    }

    public static ResizableIcon resizableIcon(String resource) {
        return ImageWrapperResizableIcon.getIcon(R.class.getClassLoader()
            .getResourceAsStream(resource), new Dimension(48, 48));
    }
}

I call it when creating a GUI

JCommandButton connect = new JCommandButton(i18ln.getString("ports"),
                R.resizableIcon(R.png("serial-port-32x32.png")));

, , R.class.getClassLoader().getResourceAsStream sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream.

. , , . ?

FYI: , , Flamingo .

EDIT: Stefan

src/
    main/
        java/
            com.company.project (packages)
                R.java
                MyGui.java
        resources/
            com.company.project (packages)
                .png (package)
                    serial-port-32x32.png
                    (more images)
                .i18ln (package)
                    MyGui.properties

, , , . , , . - , ?

UPDATE:

Jar Eclipse , . Jar, Gradle, . , - -, Eclipse Jar, Gradle Jar. Gradle .

+3
5

, (Standalone, ApplicationServer), ClassLoader.

, Utils, - :

/* Returns a instance of InputStream for the resource */
public static InputStream getResourceAsStream(String resource) 
                                                   throws FileNotFoundException {
    String stripped = resource.startsWith("/")?resource.substring(1):resource;
    InputStream stream = null;
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader != null) {
        stream = classLoader.getResourceAsStream(stripped);
    }
    if (stream == null) {
        stream = Utils.class.getResourceAsStream(resource);
    }
    if (stream == null) {
        stream = Utils.class.getClassLoader().getResourceAsStream(stripped);
    }
    if (stream == null) {
        throw new FileNotFoundException("Resource not found: " + resource);
    }
    return stream;
}

:

Utils.getResourceAsStream("com/company/project/png/serial-port-32x32.png");
+3

ECLIPSE. , ,

ClassLoader, , Java Doc, " , .

+2

, . , getResource i.o. getResourceAsStream ( : null, ).

getResource (AsStream) , , :

R.class.getResource("/" + resource)
+2

:

public final class Resources {

    public static ImageIcon getImage(String filename) {
        URL resourceUrl = Resources.class.getResource(filename);
        return new ImageIcon(resourceUrl);
    }
}

Edit:

maven flamingo ( R), :

:

R.class.getClassLoader().getResourceAsStream(resource);

R.class.getResource("png/"+resource);

maven-assembly-plugin , .

+1
URL imageurl = getClass().getResource("/images/serial-port-32x32.png");//relative path of the image as argument 
Image myPicture = Toolkit.getDefaultToolkit().getImage(imageurl);

'myPicture' ImageWrapperResizableIcon.getIcon

0

All Articles