Images will not work in .jar file

When an application JAR is created, images in the application are no longer displayed. Example of our code for uploading images:

ImageIcon placeHolder = new ImageIcon("src\\Cards\\hidden.png");

We do not know why this is happening. An application works as expected unless we compress it to a JAR; like a jar, images just fade away. We also tried using URLs instead of ImageIcons, but that just causes the program to not start at all.

Any ideas?

EDIT: we put image files in our JAR file in the correct paths so that there is no problem.

+2
source share
3 answers

API , . , , - - JAR, , , , .

, :

byte[] buffer = new byte[IMAGE_MAX_SIZE];
InputStream imageStream = getClassLoader().getResourceAsStream("src\Cards\hidden.png");
imageStream.read(buffer, 0, IMAGE_MAX_SIZE);
ImageIcon placeHolder = new ImageIcon(buffer);

, , .

+7

:

 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("Cards/hidden.png")

, , , JAR .

URL.

+2

Question : Is there a folder srcin the bank?

Tip . You can open .jar with any unzip program that supports ZIP to see its contents.

The answer . The way you refer to the resource is wrong, you should do something like getClass().getClassLoader().getResource("Cards/hidden.png").

+1
source

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


All Articles