0

So I have created a java application in eclipse and I use javafx, and then exported it in a runnable jar file. However, when I run my .jar file it gives the error of not finding my images. It works perfectly fine when I run inside eclipse.

My file structure: file structure

In UserPane I have a function that takes the image name as "title" and returns the imageview:

InputStream is=null;
    
    try {
        is=new FileInputStream("./Images/"+title+".jpg");
    } catch (FileNotFoundException e) {
        
        e.printStackTrace();
    }
    
    
    Image img=new Image(is);
    ImageView imgview=new ImageView(); 
    
    imgview.setImage(img); 
    imgview.setFitHeight(300); 
    imgview.setFitWidth(300); 

    return imgview;

I have tried some solutions, but it won't run in eclipse. I need it to run in eclipse and from the .jar. I need it to be set in the imageview please.

Update: I did not use any of the suggestions mentioned because none of them were helpful, none of them worked. What worked for me is moving my .jar in the same folder where my images folder is, while still keeping the above code.

Thank you

1 Answers1

3

FileInputStream is called that because it operates on files. In java, a file means exactly what it says: A file is a thing on a disk someplace.

An entry in a jar file is NOT ITSELF a file!

Therefore, when you write new FileInputStream? You lost the game. That can never work.

What you're looking for is a thing that lets you ask the JVM to give you resources from the same place the JVM is loading your class files.

Fortunately, that exists!

URL url = UserPane.class.getResource("/Images/" + title + ".png");

This gets you a url object, which you can pass to e.g. ImageIcon.

If you want to instead read it directly:

try (InputStream in = UserPane.class.getResourceAsStream("/Images/" + title + ".png")) {
   // use 'in' here. It's `null` if the resource doesn't exist.
}

For this specific use case, you're looking for the URL variant. You don't need the InputStream at all.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Are you sure this question hasn't been answered yet? – howlger May 25 '21 at 12:37
  • Thank you. How would I pass the url object to a javafx image or imageview? Also InputStream in = UserPane.class.getResource("/Images/" + title + ".png") does not work as UserPane.class.getResource("/Images/" + title + ".png") returns a url object – Candyfloss May 25 '21 at 12:41
  • [_"An entry in a .jar file is not itself a file"_, _"... never work"_, ...](https://stackoverflow.com/a/40554083/6505250) – howlger May 25 '21 at 12:43
  • @howlger the question and answer you directed me to does not work – Candyfloss May 25 '21 at 12:55
  • @Candyfloss When you put the images in the same package than the `UserPane` class, it's `UserPane.class.getResource(title + ".png")`. – howlger May 25 '21 at 13:25
  • @howlger so I have to put all my images in the same folder as UserPane? – Candyfloss May 25 '21 at 14:32
  • @Candyfloss Yes or you have to adapt the path in `getResource()`. – howlger May 25 '21 at 14:55