【问题标题】:JavaFX Font.loadFont() always null when using Maven使用 Maven 时,JavaFX Font.loadFont() 始终为空
【发布时间】:2018-12-24 16:17:54
【问题描述】:

我一直在尝试修复我遇到过的最困难的错误,任何可以提供帮助的人都会获得 10 个 cookie。我制作了两个相同的 JavaFX 应用程序,一个使用 Maven,一个没有构建工具。没有构建工具的版本按预期运行,Maven 应用程序总是在 Font.loadFont() 上返回 null,因此它会引发 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");

    }

}

以上代码的文件结构、编译、执行:


现在是有问题的部分

以下是使用 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>

以及目录结构:

最后,输出(由于 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。

【问题讨论】:

  • 你可以试试“/test/font/Raleway-Medium.ttf”还是只试试“/Raleway-Medium.ttf”
  • Maven 声明了严格的项目结构。据她介绍,资源(未编译的文件)分别放在目录src/main/resourcessrc/main/filters)和目录testssrc/test/resourcessrc/test/filters)中。如果不满足,maven不会将它们复制到目标目录
  • 如果你的字体和App类在同一个包里,应该是getClass().getResource("Raleway-Medium.ttf").toExternalForm(),不带任何斜线,和不带maven一样
  • DarkMental(部分)和 wallek876 都是正确的:当您使用 getResource(String) 而不使用前导 / 时,它会搜索 relative Class。资源的路径应该是"Raleway-Medium.ttf""/test/font/Raleway-Medium.tff"。mr mcwolf 也是正确的。Maven 只识别.java 内的.java 文件src/main/java 这意味着你的资源不会复制到target/classes 目录。target/classes/test/font/Raleway-Medium.tff 出现在您的目录结构图像中的事实实际上很奇怪。您是否手动将其放在那里?
  • 以上都是正确的。我通过创建资源文件夹、将字体放在那里并在 URI 前面添加 / 解决了这个问题。谢谢大家!

标签: java maven javafx fonts


【解决方案1】:

如果您使用的是 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>

【讨论】:

    【解决方案2】:

    Maven 默认有一个单独的资源目录:

    src/main/resources/test/font/Raleway-Medium.ttf
    

    然后将资源复制到路径为/test/font/Raleway-Medium.ttf的jar中。

    【讨论】:

      猜你喜欢
      • 2016-06-01
      • 2018-09-19
      • 2017-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 2016-11-20
      • 2015-08-26
      相关资源
      最近更新 更多