Java: When a program is .jar 'ed, does it no longer read images in a jar file?

I need it to work without exporting files to a computer.
At the moment, my code for storing images is:

ImageIcon icon = new ImageIcon("images\\images2.gif");

It cannot be an image, since I am adding it to JLabel.
When I jar the entire program, it stores image files in the bank.
When I go to run the real problem, there are no images.

Again, I can’t just leave .jar in the image folder already. It should work on a separate computer by itself.

+3
source share
7 answers

You want to get the image through the system class loader:

URL url = ClassLoader.getSystemClassLoader().getResource("images/images2.gif");
Icon icon = new ImageIcon(url)

images is at the root of the path of the path.

, Java (/) (\ Windows).

+10

-... - :

InputStream is = this.getClass().getClassloader().getResourceAsStream("images/image.ico");

UPD: , JARed, .

+3

:

ImageIcon icon = new ImageIcon(this.getClass().getClassloader().getResource("images/images2.gif"));

, this.getClass(). getClassloader() MyClass.class, MyClass - .

, , Eclipse, , .

: Eclipse, :

ImageIcon icon = new ImageIcon(this.getClass().getClassloader().getResource("bin/images/images2.gif"));

, , this.getClass(). getClassloader() MyClass.class. , "bin" "src". , .

0

, File , , , .

jar zip , File . Java "", " , , , , ". , , URL-.

0

Here is a handy utility class that you can use to load image resources. The log4j logger can be removed from the changed to anything that is more suitable.

public class ResourceLoader {

    private static final Logger logger = Logger.getLogger(ResourceLoader.class);

    public static Image getImage(final String pathAndFileName) {
         try {
              return Toolkit.getDefaultToolkit().getImage(getURL(pathAndFileName));
        } catch (final Exception e) {
              logger.error(e.getMessage());
              return null;
         }
    }

    public static ImageIcon getIcon(final String pathAndFileName) {
         try {
             return new ImageIcon(getImage(pathAndFileName));
         } catch (final Exception e) {
             logger.error(e.getMessage());
             return null;
         }
    }

    public static URL getURL(final String pathAndFileName) {
         return Thread.currentThread().getContextClassLoader().getResource(pathAndFileName);
    }
}
0
source

I believe images2.gif is inside the package images

URL imageurl = getClass().getResource("/images/images2.gif"); 
Image myPicture = Toolkit.getDefaultToolkit().getImage(imageurl);
JLabel piclabel = new JLabel(new ImageIcon( myPicture ));
piclabel.setBounds(0,0,myPicture.getWidth(null),myPicture.getHeight(null));
0
source

If your images are arranged in this way.

enter image description here

Then this should complete the task.

 private String imageLocation "/images/images2.png";


private ImageIcon getImageIconFromJar(String imageLocation)
{
    try
    {
        BufferedImage bi = ImageIO.read(this.getClass().getResource(imageLocation));
        ImageIcon ic = new ImageIcon(bi);

        return ic;
    } catch (IOException e)
    {
        System.out.println("Error: "+e);
    }
    return null;
}
0
source

Source: https://habr.com/ru/post/1724846/


All Articles