使用 Maven 时,JavaFX Font.loadFont() 始终为 null

标签 java maven javafx fonts

我一直在努力修复我遇到过的最困难的错误,任何可以提供帮助的人都会得到 10 个 cookie。我制作了两个相同的 JavaFX 应用程序,一个使用 Maven,另一个没有构建工具。在没有构建工具的情况下创建的那个按预期运行,其中 Maven 应用程序总是在 Font.loadFont() 上返回 null,因此它抛出空指针异常。我已将问题简化为非常简单的应用程序。

这是不使用 Maven 的应用程序:

import javafx.application.Application;
import javafx.scene.text.*;
import javafx.stage.Stage;

public class AppWithoutMaven extends Application {

    public static void main(String[] args) {

        launch(args);

    }

    @Override public void start(Stage stage) {

        Font.loadFont(AppWithoutMaven.class.getResource("Raleway-Medium.ttf").toExternalForm(), 50);

        for (String family : Font.getFamilies())
            if (family.equals("Raleway Medium"))
                System.out.println("The font is loaded");

    }

}

以上代码的文件结构、编译和执行: enter image description here


现在是有问题的部分

这是使用 Maven 的项目中几乎相同的代码:

package test.font;

import javafx.application.Application;
import javafx.scene.text.*;
import javafx.stage.Stage;

public class App extends Application {

    public static void main(String[] args) {

        launch(args);

    }

    @Override public void start(Stage stage) {

        Font.loadFont(App.class.getResource("test/font/Raleway-Medium.ttf").toExternalForm(), 50);

        for (String family : Font.getFamilies())
            if (family.equals("Raleway Medium"))
                System.out.println("The font is loaded");

    }

}

这是 pom.xml,这可能是我搞砸的地方:

<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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>test.font</groupId>
        <artifactId>font-test</artifactId>
        <packaging>jar</packaging>
        <version>1.0-SNAPSHOT</version>
        <name>font-test</name>
        <url>http://maven.apache.org</url>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

目录结构:

enter image description here

最后,输出(由于 NullPointerException 导致的运行时错误):

daniel@daniel-MS-7B22:~/Programs/fonttest/font-test$ mvn -e exec:java -Dexec.mainClass="test.font.App"
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/usr/share/maven/lib/guice.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building font-test 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ font-test ---
Exception in Application start method
[WARNING] 
java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1 (LauncherImpl.java:973)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2 (LauncherImpl.java:198)
    at java.lang.Thread.run (Thread.java:844)
Caused by: java.lang.NullPointerException
    at test.font.App.start (App.java:17)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9 (LauncherImpl.java:919)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11 (PlatformImpl.java:449)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$9 (PlatformImpl.java:418)
    at java.security.AccessController.doPrivileged (Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$10 (PlatformImpl.java:417)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run (InvokeLaterDispatcher.java:96)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop (Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11 (GtkApplication.java:277)
    at java.lang.Thread.run (Thread.java:844)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.537 s
[INFO] Finished at: 2018-07-17T00:33:07-04:00
[INFO] Final Memory: 10M/40M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project font-test: An exception occured while executing the Java class. Exception in Application start method: NullPointerException -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project font-test: An exception occured while executing the Java class. Exception in Application start method
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:309)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:194)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:107)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:955)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:194)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:564)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while executing the Java class. Exception in Application start method
    at org.codehaus.mojo.exec.ExecJavaMojo.execute (ExecJavaMojo.java:339)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:309)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:194)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:107)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:955)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:194)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:564)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1 (LauncherImpl.java:973)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2 (LauncherImpl.java:198)
    at java.lang.Thread.run (Thread.java:844)
Caused by: java.lang.NullPointerException
    at test.font.App.start (App.java:17)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9 (LauncherImpl.java:919)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11 (PlatformImpl.java:449)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$9 (PlatformImpl.java:418)
    at java.security.AccessController.doPrivileged (Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$10 (PlatformImpl.java:417)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run (InvokeLaterDispatcher.java:96)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop (Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11 (GtkApplication.java:277)
    at java.lang.Thread.run (Thread.java:844)
[ERROR] 
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

我知道这是一个很奇怪的问题,可能有一个简单的答案。我是 Maven 的新手,所以感谢您的耐心等待。如果值得一提,我正在使用 Ubuntu 18。

最佳答案

如果您使用的是 maven,那么它会尝试从 resources 文件夹中获取资源文件,如果它们不存在,它会为您提供 NPE,因为它没有'找到资源。一种解决方案是将字体放入资源文件夹。

另一种解决方案是在您的 pom.xml 中插入以下内容,然后您可以将您的文件放入任何包中。 我没有尝试使用这些字体,但我使用了 .fxml.css 文件,它对我有用。

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <targetPath>${project.build.outputDirectory}</targetPath>
            <includes>
                <include>**/*.ttf</include>
                 <!--here you can also insert your .fxml files or .css files for example-->
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
</build>

关于使用 Maven 时,JavaFX Font.loadFont() 始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51373307/

相关文章:

java - Maven 不查看本地存储库

maven - 在Heroku中的Grails应用上制作新的文物

java - hibernate HqlL : Count and Group By including zero?

java - Java 中的列表不起作用

java - 类路径上的 asm 的 cobertura 问题

java - 执行gradle构建的JAR时出现简单的Json ClassNotFoundException

java - 如何在FXML Controller 中填充javafx.TableView?

java - 避免在目标/生成源的循环中构建项目

java - 使用 jni4net 传递的两种方式对象(是 : Ignore Missing Class(es))

JavaFX:在按键事件上获取 TableView 中的目标行索引