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

fabian
  • 80,457
  • 12
  • 86
  • 114
Patrik Horváth
  • 143
  • 1
  • 11
  • 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? – GhostCat Dec 02 '16 at 14:13
  • Maybe you didn't include your **res** folder look here [link](http://stackoverflow.com/questions/18091046/creating-runnable-jar-with-external-files-included) – Bo Halim Dec 02 '16 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? – Patrik Horváth Dec 02 '16 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**! – GhostCat Dec 02 '16 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.) – fabian Dec 02 '16 at 14:19
  • @fabian its writed in BlueJ ( IDE ) for school homework , and in BlueJ u need just put it into Project folder cause there is no add resources into package – Patrik Horváth Dec 02 '16 at 14:24
  • 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. – SedJ601 Dec 02 '16 at 14:26
  • @Sedrick Jefferson lol works :3 nice – Patrik Horváth Dec 02 '16 at 14:27
  • I am going to create an answer. You can mark it as correct. – SedJ601 Dec 02 '16 at 14:29

2 Answers2

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.

fabian
  • 80,457
  • 12
  • 86
  • 114
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.

SedJ601
  • 12,173
  • 3
  • 41
  • 59