Search code examples
javafxjarexecutablegetresource

executable jar not opening when used with getResources (but works with an absolute file path) in JavaFX


Problem: Changing two lines of code makes the executable jar go from opening and working perfectly to not opening (even though it runs in Eclipse).

Description: I have made an application in JavaFX using local file directories (silly I know) to access a .jpg file and I can deploy it as an executable and it works great on my computer using this code snippet.

Obviously, another computer will be unable to access my files so I converted the files into a more universal form so anyone who uses the application would be able to view the file.

A problem arises somehow when I create a .jar executable. The .jar opens and works great when using the absolute path but when I change only 2 lines the .jar executable will not open.

Here is the full code.

package workingSamples;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ImageDisplay extends Application {

@Override
public void start(Stage stage) {
    stage.setTitle("HTML");
    stage.setWidth(500);
    stage.setHeight(500);
    Scene scene = new Scene(new Group());
    VBox root = new VBox();    

    final ImageView selectedImage = new ImageView(); 

    File file = new File( "C:\\Users\\Documents\\logo.jpg" );
    Image image1 = new Image( file.toURI( ).toString( ), 500, 200, true, true );

    //Image image1 = new Image(ImageDisplay.class.getResourceAsStream("logo.jpg"), 500, 200, true, true);        
    selectedImage.setImage(image1);

    root.getChildren().addAll(selectedImage);
    scene.setRoot(root);
    stage.setScene(scene);
    stage.show();
}

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

This .jar executes and opens with no errors but if I uncomment the one commented line (and comment out the two above it) the project works in Eclipse but it will not open any time when I export as an executable. And I get this error every time when I try to export it:

Exported with compile warnings: WorkingSamples/src/workingSamples/ImageDisplay.java

I have tried storing the data in both the bin and src, restarting Eclipse, making sure Debug mode is not running, trying every type of library handling. Maybe it is a permission issue but I do not know how to try to fix that.

Any help would be greatly appreciated! Thank you in advance :)

(I have tried the fixes from javafx jar builds fine but doesn't open (unfortunately I do not have command line access due to company security constraints) and Can't run javafx JAR outside Netbeans (I don’t believe there are any libraries that need to be imported but are not) and Unable to run Javafx from computer by double clicking the jar file (I have the latest version of Java installed) and Eclipse - error executable jar file (I tried all library handling options but maybe it’s a permission issue which I do not know how to fix))

Code inspired from this example:

ANT file output:

<?xml version="1.0" encoding="UTF-8"?>
<project name="Create Runnable Jar for Project WorkingSamples with Jar-in-  Jar Loader" default="create_run_jar">
<!--this file was created by Eclipse Runnable JAR file Export Wizard-->
<!--ANT 1.7 is required-->
<!--define folder properties-->
<property name="dir.buildfile" value="."/><property name="dir.workspace" value="C:/Users /Documents/Eclipse"/><property name="dir.jarfile" value="${dir.buildfile}"/><target name="create_run_jar"><jar destfile="${dir.jarfile}/workingSamples7.jar"><manifest><attribute name="Main-Class" value="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader"/><attribute  name="Rsrc-Main-Class" value="workingSamples.ImageDisplay"/><attribute  name="Class-Path" value="."/><attribute name="Rsrc-Class-Path" value="./  org.eclipse.fx.ide.css.jfx8_2.3.0.201603030809.jar"/></manifest><zipfileset  src="jar-in-jar-loader.zip"/><fileset  dir="${dir.workspace}/WorkingSamples/bin"/><zipfileset dir="C:/Users /Documents/   eclipse-java-mars-R-win32-x86_64/eclipse/plugins"    includes="org.eclipse.fx.ide.css.jfx8_2.3.0.201603030809.jar"/></jar></target>   </project>

Solution

  • The base directory of getResourceAsStream(...) is your jar root. I would suggest to place your image into a resource folder (in your case) WorkingSamples/resources/logo.jpg afterwards you can access the image with

    Image image1 = new Image(ImageDisplay.class.getResourceAsStream("/resources/logo.jpg"), 500, 200, true, true);
    

    Another thing you can check is if the ant script puts the image into the right place inside of the jar