Images not visible when opened with jar. After upgrading javaFx to create new image requires the full directory

heyza22 :

After upgrading IntelliJ java and javaFx to 11 and gradle to 5.1.1. When creating new Image I need to state the full directory like so:

new Image("file:src/main/java/sample/image.png");

Where before I could simply do:

new Image("image.png")

Any idea why the project isn't recognizing my packages correctly for new Image("image.png") it is giving me this error, but before the updates it was working fine:

java.lang.IllegalArgumentException: Invalid URL or resource not found

What I want to accomplish is making the images and fxml visible when executing the jar. I am creating a jar file and when I specific the path like this for the fxml or image:

FXMLLoader loader = new FXMLLoader();
loader.setLocation(new URL("file:src/main/java/sample/login.fxml"));
Pane root = loader.load(); 

When I run the jar I get:

Caused by: java.io.FileNotFoundException: src\main\java\sample\login.fxml (The system cannot find the path specified)

How can I make it so that when I pack it as a jar it can find the files, also why before the update I could simply write: new Image("image.png") and now I need to write: new Image("file:src/main/java/sample/image.png")? Any help would be appreciate!

ivaylo :

Configure a resource folder then add your images there. After building the jar if you check the files inside(you can do that by converting it to zip) and the images are not there, you should include them first. You do so by including the directory content when building the artifacts and add the recource folder: enter image description here

Now you can add you images as a resource:

Image image = new Image(getClass().getResourceAsStream("/image.png"));

Also when you open your Project Structure in Modules the resource folder should be configured as resource folder: enter image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=160486&siteId=1