Java code to load resource image or file located inside jar example

In this tutorial, let us see how to load an resource (eg. image, file ..) which is located inside an Jar file. In this example, we will create a Javafx scene and load an image or text file content in a label.

Running the below code will create a javafx scene which will have two labels and two buttons. Clicking on the button1 (load image) will load the image located inside the jar in the lable1. Clicking on the button2 (load file) will load the content of the text file located in the resources folder in the label2.

Project Structure:

 

Util.java

Code to load image or text file content  which is located inside jar file.

 

To test the application, create runnable jar and run the jar file.

 Clicking on the load image button, will load the image on label 1.   Clicking on the load text button will load the content of the text file in the second label.  Please ensure that test.png is placed inside image folder and test.txt is placed in resources folder.

 

Leave a Reply