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:

package javaonline.javafx.ui.test;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javaonline.javafx.util.Utils;



public class JavaFxGui extends Application {
	

	public static int WINDOW_WIDTH = 1200;
	public static int WINDOW_HEIGHT = 500;

	

	public static void main(String[] args) {
	
		launch(args);
			}
	

	@Override
	public void start(final Stage stage) {

		stage.setTitle("JavaFx Example");

		GridPane grid = new GridPane();
		grid.setHgap(1);
		grid.setVgap(12);
		grid.setAlignment(Pos.TOP_CENTER);
		ColumnConstraints column = new ColumnConstraints();
		column = new ColumnConstraints();
		column.setPercentWidth(100);
		grid.getColumnConstraints().add(column);

		
		Label lbl = new Label("Image Loading Test");
		
		Label lbl1 = new Label("Text Loading Test");

		Button btn = new Button();
		btn.setText("Load Image");

		Button btn1 = new Button();
		btn1.setText("Load File");

		
		btn.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				
				
				    //Loading Image from classpath 

				      lbl.setGraphic(Utils.setIcon("/image/test.png"));
				      
					   //URL url = this.getClass().getResource("/image/test.png");
					    //lbl.setGraphic(Utils.setIcon(url));

				    
		}
			
		});
		
		
		btn1.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
			
//					load an text file located inside jar
				    	
				   	lbl1.setText(Utils.loadTextFromFile("/resources/test.txt"));
				    	
			
		}
			
		});
		
		
		final VBox vbox= new VBox();
    	vbox.setSpacing(5);
		vbox.getChildren().addAll(lbl,lbl1, btn,btn1);
		grid.add(vbox, 0, 0);
		Scene scene1 = new Scene(grid, 200, 300);
		stage.setScene(scene1);
		//stage.getIcons().add(Utils.setIcon("/image/test.png").getImage());
		stage.getIcons().add(Utils.setImage("/image/test.png"));
		stage.show();

		
	}

	

}

 

Util.java

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

package javaonline.javafx.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;

import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;

public class Utils {
	
	
	
	
	public static Image setImage(String path) {
		
		InputStream is=Utils.class
				.getResourceAsStream(path);
		return new Image(is);
	}
	
	
	
	//To load image located inside jar
			
		public static ImageView setIcon(String path) {
		
			InputStream is=Utils.class
					.getResourceAsStream(path);
			ImageView iv = new ImageView(new Image(is));

			iv.setFitWidth(100);
			iv.setFitHeight(100);
			return iv;
		}
		
		
		
		
		public static ImageView setIcon(URL url) {
			
				 File file=null;
				    InputStream is = null;
					  
					try {
						file = new File(url.toURI());
						 if(file.isFile())
							 is = new FileInputStream(file);
					} catch (URISyntaxException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				
						
			ImageView iv = new ImageView(new Image(is));

			iv.setFitWidth(100);
			iv.setFitHeight(100);
			return iv;
			// return new ImageView(image);
		}		
		
		
		
		public static String loadTextFromFile(String path) {
			
			InputStream text=null;
	
				try {
					text = (InputStream) Utils.class.getResource(path).getContent();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				java.util.Scanner utilScanner = new java.util.Scanner(text).useDelimiter("\\A");
			    return utilScanner.hasNext() ? utilScanner.next() : "";
			
			
			
			// return new ImageView(image);
		}
		
		
		

 

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