在 JavaFX 中与 getResources(但使用绝对文件路径)一起使用时无法打开可执行 jar

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

问题: 更改两行代码使可执行 jar 从打开并完美运行到无法打开(即使它在 Eclipse 中运行)。

描述: 我已经使用本地文件目录(我知道这很愚蠢)在 JavaFX 中创建了一个应用程序来访问 .jpg 文件,我可以将其部署为可执行文件,并且使用此代码片段在我的计算机上运行良好。

显然,另一台计算机将无法访问我的文件,因此我将文件转换为更通用的形式,以便使用该应用程序的任何人都能够查看该文件。

当我创建 .jar 可执行文件时不知何故出现了问题。 .jar 在使用绝对路径时打开并运行良好,但当我仅更改 2 行时,.jar 可执行文件将无法打开。

这是完整的代码。

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);
}
}

此 .jar 文件执行并打开时没有错误,但如果我取消注释注释行(并注释掉上面的两行),该项目在 Eclipse 中工作,但当我导出为可执行文件时它不会打开。我每次尝试导出时都会收到此错误:

导出时带有编译警告:WorkingSamples/src/workingSamples/ImageDisplay.java

我已经尝试将数据存储在 bin 和 src 中,重新启动 Eclipse,确保调试模式不是 运行,尝试每种类型的库处理。也许这是一个权限问题,但我不知道如何解决。

如有任何帮助,我们将不胜感激!提前谢谢你:)

(我已经尝试了 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 的修复(我尝试了所有库处理选项,但可能是权限问题,我不知道如何修复))

代码灵感来自 this example:

ANT 文件输出:

<?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>

getResourceAsStream(...) 的基本目录是您的 jar 根目录。我建议将您的图像放入资源文件夹(在您的情况下)WorkingSamples/resources/logo.jpg 之后您可以使用

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

您可以检查的另一件事是 ant 脚本是否将图像放入 jar 中的正确位置