0

I use javaFX without a fxml file needed all time. When I run my app in IDE it works perfectly. Only when I make jar file it does not work. Any help?

error : no such file in res\img\menu_bg.png 

this file is in same folder like whole code

code

public class InputStreamClass {
    private ImageView imgViewBG;
    public InputStreamClass() throws Exception {
        InputStream isBG = Files.newInputStream(Paths.get("res/img/menu_bg.png")); // res/img/menu_bg.png
        Image imgBG = new Image(isBG);
        isBG.close(); // kedze je to InputStream a pracujeme zo suborom tak vzdy ho treba zatvarat 
        this.imgViewBG = new ImageView(imgBG); // pridame img do ImageView
        this.imgViewBG.setFitWidth(1600); // sirka obrasku
        this.imgViewBG.setFitHeight(900); // vyska obrasku
    }
     public ImageView getImageViewBG() {
        return this.imgViewBG;
     }
}

enter image description here

9
  • A) your stack trace is text. So bring it in as text, and not as close-to-impossible-to-read-for-senior-people screenshot. B) did you ensure that your JAR file really contains the resources you want to use? Commented Dec 2, 2016 at 14:13
  • Maybe you didn't include your res folder look here link Commented Dec 2, 2016 at 14:15
  • @ghostCat just click on it it popup and u can zoom it :) and i included packgaes only need include resources too ? it douesnt include automaticaly with Package? Commented Dec 2, 2016 at 14:16
  • Fair enough, but still: screen shots are seen as "last resort" around here. For things that only can be screen shoted. Your console output is text! Commented Dec 2, 2016 at 14:18
  • 1
    It does not matter whether the files are in the same folder as the code. What does matter is if the relative path res/img/menu_bg.png can be resolved from the working directory. Furthermore a file that is used all the time should be included as resource(assuming you do not have to write to it.) Commented Dec 2, 2016 at 14:19

2 Answers 2

4

I assume you do not need to write to the image file.

In this case the image should better be included in the jar itself. This prevents issues with paths. Java tries to resolve relative paths relative to the working directory, which often is not the code folder.

By adding the image to the jar file itself you can get the resource using a Class instance, e.g.

// InputStream isBG = getClass().getResourceAsStream("/res/img/menu_bg.png");
// Image imgBG = new Image(isBG);

Image imgBG = new Image(getClass().getResource("/res/img/menu_bg.png").toExternalForm());

which works independent of the working directory.

This assumes the file is stored as /res/img/menu_bg.png entry in the jar file.

Sign up to request clarification or add additional context in comments.

Comments

1

try this: In the same folder as your jar, put another folder name res. Inside the res folder create a folder name img. Inside the img folder put your actually image with the name above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.