JavaFX Font.loadFont() always null when using Maven

user3720913 :

I've been trying to fix the most difficult bug I've ever had and anyone that can help gets 10 cookies. I've made two identical JavaFX applications, one using Maven and one with no build tools. The one that was made without a build tool runs as expected, where the Maven app always returns null on Font.loadFont() and thus it throws a Null Pointer Exception. I've reduced the problem to very simple apps.

Here is the app that doesn't use 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");

    }

}

The above code's file structure, compilation, and execution: enter image description here


Now for the problematic part

Here is the nearly identical code from the project USING 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");

    }

}

Here is the pom.xml, which is probably where I messed up:

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

And the directory structure:

enter image description here

And lastly, the output (Runtime error due to 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

I know this is such a weird question and there probably is an easy answer to it. I'm sorta new to Maven so thanks for your patience. If it's worth mentioning, I'm on Ubuntu 18.

Sunflame :

If you are using maven, then it tries to get the resource files from the resources folder, and if they aren't there it gives you NPE since it doesn't find the resource. One solution would be to put the font into the resources folder.

Another solution would be to insert the following in your pom.xml then you can let your files in any package. I haven't tried using the fonts but I did with.fxml and .css files and it works for me.

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=76420&siteId=1