1

I'm trying to export a java project that includes som images, but the images don't show up after exporting it. I've read these similar questions and answers:

Eclipse exported Runnable JAR not showing images

Exporting Images with JAR in Eclipse (Java)

I've tried all the different options that are suggested but the images still don't show up after exporting the project.

I've tried this file structure:

enter image description here

With this structure I create the image with this code:

Image picture = new Image("file:src/Pictures/download.png");

And this file structure:

enter image description here

With this structure I create the image with this code:

Image picture = new Image("file:Pictures/download.png");

When exporting the project I've chosen "Package required libraries into generated JAR":

enter image description here

The images show up when I run the program from Eclipse, but as soon as I export it they disappear. What am I doing wrong? I really feel like I have tried everything, but maybe there's some simple detail that I'm missing?

Thanks!

Community
  • 1
  • 1
ekstroom
  • 107
  • 1
  • 7

1 Answers1

1

The problem is not with creating the jar file, it is with the way you are reading the image, do read the image in your java code as below it will work.

ImageIO.read( ClassLoader.getSystemResource( "image/button1.png" ) );
Sanjit Kumar Mishra
  • 1,153
  • 13
  • 32
  • Ok, could you explain that some more? I'm quit new to javafx, so I'm no expert ;) That code seems to return a buffered image. I want to put my image into an image view to display it in the program. But when creating an image view it seems you can not use a buffered image as the input parameter. How do I make the buffered image appear in the program? – ekstroom Aug 18 '16 at 07:10
  • I used this code and now it works fine: ImageView imgViewPicture = new ImageView(); try { BufferedImage buffered = ImageIO.read( ClassLoader.getSystemResource("Pictures/download.png" )); Image picture = SwingFXUtils.toFXImage(buffered, null); imgViewPicture = new ImageView(picture); imgViewPicture.setPreserveRatio(true); imgViewPicture.setFitHeight(250); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Thank you for your help! :) – ekstroom Aug 18 '16 at 07:19
  • `InputStream stream = getClass().getResourceAsStream("/resources/icons/xyz.png"); ImageIcon icon= new ImageIcon(ImageIO.read(stream))` . You can do `icon.getImage()` to get the image object – Sanjit Kumar Mishra Aug 18 '16 at 07:29