【问题标题】:After Compiling Project With Javafx Maven Plugin, Program Cant Locate Resources使用 Javafx Maven 插件编译项目后,程序无法找到资源
【发布时间】:2016-02-14 10:39:57
【问题描述】:

好吧,我对 javafx 和一般编程相当陌生。我以前从未真正打包过程序,因为我从来不需要在 IDE 之外运行它们。我需要打包我的 javafx 程序,并且必须使用 maven 来处理我的所有其他依赖项。

在包含 pom 文件的目录中使用命令 mvn jfx:jar 编译程序后,构建成功并生成我的 jar。通过 WINRar 检查时,其中包含以前在我的根目录中的 /src/main/resource 文件夹中的所有文件。

但是,当使用 java -jar (nameofjar).jar 运行时,它不会启动,而是会产生几个与无法定位特定资源有关的错误,尤其是 FXML,以及其他资源。

文件参考

welcome.fxml

public void start(Stage primaryStage) {


    try {
        AnchorPane pane = FXMLLoader.load(MainApp.class.getResource("welcome.fxml"));
        Scene scene = new Scene(pane);
        primaryStage.setScene(scene);
        primaryStage.setTitle("HeelBot Beta "+versionNumber);

        primaryStage.getIcons().add(new Image("Icon.png"));
        primaryStage.setResizable(true);
        primaryStage.show();
    } catch (Exception ex) {
        Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
    }

defaultProperties.properties

    public class PropertiesManager {
    public static void editProperty(String key, String value) throws IOException {
        // create and load default properties
        Properties defaultProps = new Properties();
        FileInputStream in = new FileInputStream("src/main/resources/defaultProperties.properties");
        defaultProps.load(in);
        in.close();

// create application properties with default
        Properties applicationProps = new Properties(defaultProps);

// now load properties
// from last invocation
        in = new FileInputStream("src/main/resources/appProperties.properties");
        applicationProps.load(in);
        in.close();
//set properties
        applicationProps.setProperty(key,value);
        FileOutputStream out = new FileOutputStream("src/main/resources/appProperties.properties");
        applicationProps.store(out, "---No Comment---");
        out.close();

    }

POM 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.coursecruncher</groupId>
    <artifactId>heelbot</artifactId>
    <version>1.0-SNAPSHOT</version>
<build>
    <plugins>
        <plugin>
            <groupId>com.zenjava</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>8.1.4</version>
            <configuration>
                <mainClass>MainApp</mainClass>
                <verbose>true</verbose>
                <jfxMainAppJarName>heelbot.jar</jfxMainAppJarName>
                <deployDir>${project.basedir}/src/main/resources</deployDir>
                <updateExistingJar>true</updateExistingJar>
                <allPermissions>true</allPermissions>
            </configuration>
        </plugin>

    </plugins>

</build>

    <dependencies>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.48.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.2.4</version>
        </dependency>
    </dependencies>

</project>

项目结构

├── configure
├── configure.in
├── src
│   ├── main
│   │   ├── java 
│   │   │     ├── CoreController(class)
│   │   │     ├── Login(class)
│   │   │     ├── LoginController(class)
│   │   │     ├── MainApp(class)
│   │   │     └── PropertiesManager(class)
│   │   │        
│   │   ├── resources
│   │   │     ├── appProperties.properties
│   │   │     ├── defaultProperties.properties
│   │   │     ├── welcome.fxml
│   │   │     ├── core.fxml
│   │   │     └── Heelbot.css 

错误

enter code here

java.io.FileNotFoundException: src\main\resources\defaultProperties.properties (
The system cannot find the path specified)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(FileInputStream.java:195)
        at java.io.FileInputStream.<init>(FileInputStream.java:138)
        at java.io.FileInputStream.<init>(FileInputStream.java:93)
        at PropertiesManager.editProperty(PropertiesManager.java:11)
        at LoginController.initialize(LoginController.java:60)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
        at Main.start(Main.java:53)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163
(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Platfor
mImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.
java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformI
mpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatch
er.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.ja
va:191)
        at java.lang.Thread.run(Thread.java:745)
java.io.FileNotFoundException: src\main\resources\defaultProperties.properties (
The system cannot find the path specified)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(FileInputStream.java:195)
        at java.io.FileInputStream.<init>(FileInputStream.java:138)
        at java.io.FileInputStream.<init>(FileInputStream.java:93)
        at PropertiesManager.queryProperty(PropertiesManager.java:34)
        at LoginController.initialize(LoginController.java:106)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
        at Main.start(Main.java:53)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163
(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Platfor
mImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.
java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformI
mpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatch
er.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.ja
va:191)
        at java.lang.Thread.run(Thread.java:745)
Nov 12, 2015 5:12:08 PM Main start
SEVERE: null
javafx.fxml.LoadException:
file:/D:/Programing%20Data/Projects/CourseCruncher/target/jfx/app/heelbot.jar!/w
elcome.fxml

        at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
        at Main.start(Main.java:53)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163
(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Platfor
mImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.
java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformI
mpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatch
er.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.ja
va:191)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
        at java.util.Properties$LineReader.readLine(Properties.java:434)
        at java.util.Properties.load0(Properties.java:353)
        at java.util.Properties.load(Properties.java:341)
        at PropertiesManager.queryProperty(PropertiesManager.java:39)
        at LoginController.initialize(LoginController.java:106)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
        ... 17 more

我怀疑我的 pom 配置可能是导致问题的原因。我不太确定如何正确格式化它,并且尝试了各种组合和教程都无济于事。我得到的最接近的是使用这个 pom

备用 POM 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.courecruncher</groupId>
    <artifactId>heelbot</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>HeelBotBeta1</name>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.48.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.2.4</version>
        </dependency>

    </dependencies>
    <build>
        <defaultGoal>clean package</defaultGoal>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>unpack-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <excludeScope>system</excludeScope>
                            <excludeGroupIds>junit,org.mockito,org.hamcrest</excludeGroupIds>
                            <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <id>package-jar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>${env.JAVA_HOME}/bin/javafxpackager</executable>
                            <arguments>
                                <argument>-createjar</argument>
                                <argument>-nocss2bin</argument>
                                <argument>-appclass</argument>
                                <argument>MainApp</argument>
                                <argument>-srcdir</argument>
                                <argument>${project.build.directory}/classes</argument>
                                <argument>-outdir</argument>
                                <argument>.</argument>
                                <argument>-outdir</argument>
                                <argument>${project.build.directory}</argument>
                                <argument>-outfile</argument>
                                <argument>${project.artifactId}-app</argument>
                                <argument>-v</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

这将导致它找到 fxml 文件,但仍然会为属性文件抛出错误,我必须手动将属性文件添加到项目目录中。如何让打包后的 jar 能够定位到自己的资源,而不是在 jar 之外寻找资源?

【问题讨论】:

  • 帖子的大小反映了有多少人有时间更好地帮助您。你最好尝试创建一个MCVE

标签: java maven javafx pom.xml packaging


【解决方案1】:

我是 javafx-maven-plugin 的维护者,偶然发现了这个问题;)

问题出在您读取该文件的“逻辑”中,在您的 JAR 中访问该属性文件是以另一种方式完成的,但这不是所用插件的错,这是一个正常的 java 问题(如所指出的)我们被抛出的异常)。要知道“您当前的文件夹”在哪里,只需打印出new File(".")

只是通知您有关 javafx-maven-plugin 配置:没有错

以下是我找到的一些链接,可向您展示如何在 JAR 文件中简单地加载属性:

Load properties file in JAR?

Load Properties from resources folder within JAR

编辑:请确保对“当前工作文件夹”有一点了解,调用 java -jar somejar.jarjava -jar somefolder/somejar.jar 不同,会有很大的不同

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    相关资源
    最近更新 更多